pranky64 pranky64 - 11 months ago 50
Javascript Question

Jquery $('#someID > div').each(function(){..}) not working in IE

i don't face this problem while working on localhost only when I access the page using the ip address of my system this happens and it only happens with IE!! (works on all other browsers)

by the way i'm using Tomcat V6.0.0.29, IE8

I tried debugging the JS code using IE developer tools debugger, ofcourse when I open using

everything works perfectly fine, but when I use
this loop is giving a problem :-

$('#someId > div').each(function(){...});

As in this loop doesn't run at all, it just kind of skips it. I have checked the IDs they are fine moreover its working in localhost why should it give a problem when I access it using my IP?

Note :-
a. (correction) The problem is only there in IE7, it works perfectly in IE8.

b. As it turns out something weird is happening! i'm using IE8 when i open this webpage using localhost the developer tools shows its working in IE8 standards but when i use the IP address to access this page the developer tools shows its working in IE7 standards. When i changed the standards to IE8 it worked (using the IP address)!

c. But the problem is why the hell is it not working with IE7!! As in everything works except the loop mentioned above.

Answer Source

Finally i came to know what was causing the problem in IE7. Consider the below situation:-

<div id="div1">abc
          <div id="div2">def
          <div id="div3">xsj

The jquery i had written for traversing these divs was something like

$("#divId > div").each(function(){..});

Now for the first level div that is traversing the divs directly inside the div with id "div1" worked perfectly in IE7, but when i did something like:-

$("#div2 > div").each(function(){..});

This worked in all browsers (even in IE8!!) but not in IE7. This is because apparently IE7 requires the exact child selector for divs. So for IE7 something like this needs to be written:-

$("#div1 > #div2 > div").each(function(){..});

for traversing the divs inside the div with id "div2"

So the problem was cause just by my lack of knowledge about IE7!! sorry n thanks guys!