tags:

views:

53

answers:

6

I have a div element which contains UL and UL contains the LI items. One of the LI item has an ID of stocknumber. I need to select that li.

Here is my code which works fine I am just looking for a better implementation.

$(".block").children("ul").children("#stocknumber") // gets me the li and it works!! 

UPDATE 1:

Please note that ids are not unique!

Here is what I came up with:

$(".block").children("ul").find("#stocknumber") 
+2  A: 
$(".block > ul > #stocknumber")

or even:

$("#stocknumber"); // because every id in your HTML should be unique
Philippe Leybaert
thats worse because of performance
jAndy
performance isn't always the most important thing. Simplicity is more important in most cases. Besides, the second one is the absolute fastest way of achieving what the OP wants
Philippe Leybaert
Indeed, the second method is better performing than any other combination of selectors would be.
Andy E
you added the second line later, so its not.
jAndy
Yeah, 30 seconds later. Indeed a good reason to downvote
Philippe Leybaert
But the first method is better than the others that use compound selectors, since it does not require full-depth searches underneath .block.
harpo
relaxe I wil remove it, but at the point I read it, it was worse than the original code from OP (still performance). nevermind
jAndy
jAndy: as I mentioned in my first comment, in most cases performance of a selector is unimportant. Clean and maintainable code is a lot more important. If it wasn't, we'd all still be coding in C++
Philippe Leybaert
since javascript (better said DOM operations) are pretty damn slow, I would think about what you just said.
jAndy
@jAndy: Philippe Leybaert's first example is far, far, far (30ms per run) quicker than the OP's original code.
Matt
how can you benchmark that without the exact markup ?
jAndy
Is this an organised voting war? @jAndy: you should read up on software architecture and performance literature. I know it is fun to squeeze the last bit of peformance out of a piece of code, but in most real-world scenarios it's usually not worth the trouble and even counter-productive in real production code.
Philippe Leybaert
Well I know about software architecture, but still, javascript and DOM operations I (maybe not you) always do prefer performance at (mostly) all costs.
jAndy
@jAndy: How about we quit being so snappy over every comment and learn to accept that occasionally you're wrong? FWIW, I benchmarked `$('#method-list').children().find('h2.entry-title')` on http://api.jquery.com, but the results will be consistent on any markup you test it on.
Matt
With all due respect, the first method can be deceiving, because it implies that you are looking for the `#stocknumber` element that is under `.block > ul`, as opposed to the others (when there shouldn't be others). It may be a personal preference for you, but I think it could mislead future readers.
patrick dw
I agree with everyone that performance is not really a concern at this stage. Considering performance for this right now is thinking about removing a single corn flake from your bowl to consume exact calories. It will not make any difference!
john doe
@Matt: of course it is faster, but ONLY because the most right selector is an ID and sizzle works from right to left, so, I'm not snappy I'm right. In any other constelation his code will be slower.
jAndy
@jAndy - I didn't know Sizzle worked right to left. That's interesting. I wonder if it is optimized such that it ignores anything to the left of an ID. Any idea?
patrick dw
That is the only explanation for Matts benchmark results, but I'm pretty sure sizzle will work exactly like that. Check the most right selecter, id? > just select it
jAndy
+3  A: 
$("#stocknumber");

Since IDs are unique, you shouldn't have more than one element with the ID of stocknumber in your page.

richsage
Actually in my case the id is not unique!
john doe
In that case you should rethink your HTML markup. IDs are required to be unique by the HTML spec :-) see http://www.w3.org/TR/html401/struct/global.html#h-7.5.2
richsage
john doe: You should do everything possible to keep ID's on a page unique.
Matt
Thanks @Matt! I think I will replace ID with class and that will do the trick as well. Thanks everyone!
john doe
A: 

I think I got my answer using the find method.

$(".block").children("ul").find("#stocknumber")
john doe
`find()` is slower than `children()`. But take everyones criticism into consideration: You need to make these ID's unique. It brings unexpected behaviour. Look at using classes over ID's. Classes don't have to be unique.
Matt
find() is slower than children()? I really do not believe that.
jAndy
@jAndy: `find()` searches all descendants. `children()` searches all children....
Matt
+1  A: 

You should definitely select it by its ID directly.

$('#stocknumber');

If you have more than one element with that same ID, it is bad, and you will have problems with your code.

In that situation, you should use classes instead of IDs, like this:

$(".block").find(".stocknumber");

or

$('.stocknumber', '.block');

With HTML like this:

<div class='block'>
    <ul>
        <li class='stocknumber'>number</li>
    </ul>
</div>

<div class='block'>
    <ul>
        <li class='stocknumber'>number</li>
    </ul>
</div>
patrick dw
@patrick: I was just simplyfiying what OP did but you do have the point and yes i need to delete it. Thanks for notifying that.
Sarfraz
@Sarfraz - OK, no offense. :)
patrick dw
+1  A: 

$('#stocknumber').

Id's should be unique to the page. You're just slowing things down.

Matt
I might not be slowing things down since I get the DIV element from a function due to some drop operation. So, I only get the exact DIV element.
john doe
+1  A: 

If that LI has an id just do

$('#stocknumber')

since ids have to be unique in the markup.

jAndy