tags:

views:

57

answers:

3

I have a large HTML document which has roughly this structure:

<div id="a">
    <!-- more html code here -->
    <span id="title">...</span>
    <!-- more html code here -->
</div>
<div id="b">
    <!-- more html code here -->
    <span id="title">...</span>
    <!-- more html code here -->
</div>
...
...
<div id="z">
    <!-- more html code here -->
    <span id="title">...</span>
    <!-- more html code here -->
</div>    

Notice that the outer DIVs have IDs which are unique ("a", "b", ..., "z"), but inner SPANs have IDs which is non-unique ("title").

To select a SPAN which is inside the DIV "q", for instance, I tried using this:

$("#q").find("#title"); 

This runs fast on FF and Chrome but the find() method takes a long time to execute in IE8 (and IE7). Is there some other way I can do this?

Please let me know if I can provide any further information.

+8  A: 

You should change your markup to use classes for the <span> elements instead of IDs since IDs must be unique.

<div id="a">
    <!-- more html code here -->
    <span class="title">...</span>
    <!-- more html code here -->
</div>
<div id="b">
    <!-- more html code here -->
    <span class="title">...</span>
    <!-- more html code here -->
</div>
...
...
<div id="z">
    <!-- more html code here -->
    <span class="title">...</span>
    <!-- more html code here -->
</div> 

then:

$("#q").find(".title");

If I remember correctly, IE in particular has trouble with non-unique IDs. Changing your code to above may do the trick for you.

patrick dw
You are right. Wish it was that way. But I have little control over those IDs. Anyways, thanks for your answer... looks like I am hosed here.
Rohit Agarwal
If you only have one span you can do it via $("#q").find("span")
Marko
There can be more than one spans. But based on what you suggest, I can try $("#q").find("span#title"). Maybe that would help a little.
Rohit Agarwal
@Rohit - If Marko's suggestion doesn't work because each `div` has more than one `span`, there may be another selector that will work. As long as there's some sort of predictability in each `div`, there's likely some selector that doesn't use IDs that will work.
patrick dw
I am going to accept this answer. I tried the other methods below, but the problem is that the document is too large with many Spans within each Div. And because of the non-unique IDs, the browser does a search through all the spans to find the one we need. Its that search that takes too much time. There is actually no performance problem on smaller documents.
Rohit Agarwal
+2  A: 

As everyone else said, the code is invalid. However, if you are stuck with it, you should be able to do this to select it correctly. Without running tests, I am not sure how performant this will be over the other method you tried:

$("#b span[id=title]");
Doug Neiner
This will work too
Marko
+1 - I just left a comment (deleted) under mine suggesting the same thing, then scrolled down and saw you beat me to it by a long shot! :o)
patrick dw
A: 

How about something like

$("#q").find("span").each(function() {
    if($(this).attr("id") == "title") {
        $(this).css('color','red');
    }
});

This works for me in IE/FF

Marko