Why does the following work in firefox but not in IE?
$("#bCompare").bind("click", function(e){
$("img[src='s1.gif']").each(function (i) {
$("#cSelected").append("<div class='cHolder'></div>");
});
});
UPDATE 1: A commenter mentioned I haven’t put in enough information, although i’m not sure what more is needed. The above code checks for the number of images with the src s1.gif. It then runs a loop inserting a div of the class cHolder into the div cSelected. The number cHolders added depends on the number of s1.gif images.
There are no error messages in either browser. Firefox 3.5 and IE 7
UPDATE 2: I’ve narrowed it down to a problem in the “each” method. Outside the loop it works fine in both. Is there any way to get around this? Thanks.
What you’ve run into is actually really interesting. I started with your code, and the results are quite confusing. The
src='s1.gif'selector works fine in Firefox and IE8, but not in IE7 or IE6. I ended up with a test document to figure out this selector:So assuming you have an image file called “foo.png” in the same directory as this test document, you get the following alerts in FF3.5, IE8, IE7, and IE6 respectively:
So the selector
$("img[src='foo.png']")clearly doesn’t find the image properly in IE6 and 7, despite thesrcbeing reported correctly by the alert. If you change the selector src to the full path to the image ($("img[src='http://whatever/foo.png']")), you get the opposite results. It’s found in IE6/7, but not in IE8 or FF.Very weird. The
srcmust be represented differently in the DOM somehow in these browsers, but not accounted for properly by JQuery.Gaby actually posted an answer that was on the right track, but then he deleted it. If you use a selector with a
$sign, likeimg[src$='foo.png'], it appears to work in all 4 of these browsers, as it matches thefoo.pngon the end of the attribute value.I would suggest you go with that method.