When using jQuery.attr() to set an attribute the browser doesn't update the style from the css -


i have site hat lists set of files can downloaded. custom attribute doctype set based on file extension. if there no extension doctype set "unknowndoc". css file looks similar this:

.titlecolumn[doctype="pdf"] {     background: url("/images/common/icons/pdf.png") no-repeat left center; }  .titlecolumn[doctype="doc"], titlecolumn[doctype="docx"] {     background: url("/images/common/icons/word.png") no-repeat left center; }  .titlecolumn[doctype="unknowndoc"] {     background: url("/images/common/icons/unknowndoc.png") no-repeat left center; } 

it's quite possible user upload document don't have css style set yet. in case item have doctype no background-image. in these cases want unknowndoc style applied. use following:

$('.titlecolumn').each(function (index) {     var hasnodocimage = $(this).css("background-image") == "none";     var doctype = $(this).attr("doctype");      if (hasnodocimage && doctype) {         $(this).attr("doctype", "unknowndoc");         $(this).addclass("titlecolumn[doctype=\"unknowndoc\"]");     } }); 

this works fine. don't understand why have use addclass statement? attr or addclass not work. it's if adding attribute doesn't cause browser re-style item based on new attribute addclass does. jquery thing or browser thing?

is there better way this?

i tried using classes rather doctype custom attribute gets messy, when additional classes may added element in future.

seems ie chrome , ff work fine without addclass. easier version of yours above is:

$(this).attr("doctype", "unknowndoc").removeclass('titlecolumn').addclass('titlecolumn'); 

seems ie's inability (or jquery's) understand dynamic changes selectors.

you can verify in fiddle ff , chrome fine: http://jsfiddle.net/fusvf//


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -