Last active
December 24, 2015 05:49
-
-
Save agaase/6753027 to your computer and use it in GitHub Desktop.
Set fixed style attribute by setting important.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.setFixedStyle = function(styles){ | |
var s = $(this).attr("style"); | |
s = "{"+s.replace(/;/g,",").replace(/'|"/g,""); | |
s = s.substring(0,s.length-1)+"}"; | |
s = s.replace(/,/g,"\",\"").replace(/{/g,"{\"").replace(/}/g,"\"}").replace(/:/g,"\":\""); | |
var stOb = JSON.parse(s); | |
var st; | |
if(!styles){ | |
$.each(stOb,function(k,v){ | |
stOb[k] +=" !important"; | |
}); | |
} | |
else{ | |
$.each(styles,function(k,v){ | |
if(v.length>0){ | |
stOb[k] = v+" !important"; | |
}else{ | |
stOb[k] += " !important"; | |
} | |
}); | |
} | |
var ns = JSON.stringify(stOb); | |
$(this).attr("style",ns.replace(/"|{|}/g,"").replace(/,/g,";")); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Usage is pretty simple.Just pass an object containing all the attributes you want to set as important. | |
e.g $("#i1").setFixedStyle({"width":"50px","height":""}); | |
There are two additional options. | |
1.To just add important parameter to already present style attribute pass empty string. | |
2.To add important param for all attributes present dont pass anything. It will set all attributes as important. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment