I can use $("ul").children("li").get(0); to get the list item. How can I use .children("a") to traverse down the tree?
I tried something like $("ul").children("li").get(0).children("a"); but that does not work.
For future reference:
I will be doing other things to the li other than traversing downto the a, which is why I am trying to use .children().Index will be dynamic and not always 0.Which is why .eq() is the preferred answer. Now I can do something like:
_load = function(index) { image_get = thumbs.children("li").eq(index); <...> $(function() { var img = $(new Image()); img .load(function() { <...> } .attr("src", image_get.children("a").attr("href")); });}_load(0);^^^ WIP. Code unfinished.
javascript jquery get children traversal
Share
Improve this question
Follow
edited Jul 26, 2011 at 4:31
gavsiu
asked Jul 26, 2011 at 4:09

gavsiugavsiu
72755 gold badges99 silver badges2626 bronze badges
Add a comment |
3 Answers 3
Sorted by: Reset to default
Highest score (default) Date modified (newest first) Date created (oldest first)
11
.get() returns a DOM Element. You can wrap it back in a jQuery object like:
$($("ul").children("li").get(0)).children("a");But I think what you really want is .eq() instead of .get():
$("ul").children("li").eq(0).children("a");Edit
As Reid kindly pointed out in the comment below,
$("ul").children("li").eq(0).children("a");
is semantically equivalent to the more concise :
$("ul > li:first-child > a");
which does the same as the above with a single selector.
Share
Improve this answer
Follow
edited Jul 26, 2011 at 4:16
answered Jul 26, 2011 at 4:10

PaulPaul
135k2525 gold badges267267 silver badges257257 bronze badges
4
Add a comment |
4
Why not just put it all in the selector and let that do all the work for you:
$("ul li:first a")This will get a jQuery object for the first tag in your sample HTML.
The selector logic works like this: Find all ul tags, the find the first li tag in each, then get all the a tags in each of the first li tags. The result is a jQuery selection which you can then apply various operations to.
Xem thêm: Lmht: Ấn Tượng Bộ Skin Cung Hoàng Đạo Lol, Mở Bán Nhóm Trang Phục Vũ Trụ
Demo here: http://jsfiddle.net/jfriend00/5qZP5/
Share
Improve this answer
Follow
edited Jul 26, 2011 at 4:21
answered Jul 26, 2011 at 4:15

jfriend00jfriend00
635k8787 gold badges894894 silver badges902902 bronze badges
Add a comment |
1
You can do this
$("ul li a:first");
http://jsfiddle.net/jasongennaro/yCAT6/
$("ul li a"); returns all of the as in that list.
:first filters for just the first.
Share
Improve this answer
Follow
edited Jul 26, 2011 at 4:21
answered Jul 26, 2011 at 4:15

Jason GennaroJason Gennaro
33.6k66 gold badges6161 silver badges8585 bronze badges
Add a comment |
Your Answer
Thanks for contributing an answer to Stack Overflow!
Please be sure to answer the question. Provide details and share your research!But avoid …
Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.
Draft saved
Draft discarded
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Submit
Post as a guest
Name
Email Required, but never shown
Post as a guest
Name
Required, but never shown
Post Your Answer Discard
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged javascript jquery get children traversal or ask your own question.
The Overflow Blog
Featured on Meta
Related
3022
Is there an "exists" function for jQuery?
3080
How can I upload files asynchronously with jQuery?
2587
Add table row in jQuery
8368
How do I check if an element is hidden in jQuery?
4515
How do you get a timestamp in JavaScript?
2350
How to get the children of the $(this) selector?
1992
Get current URL with jQuery?
4441
Setting "checked" for a checkbox with jQuery
4995
How do I check whether a checkbox is checked in jQuery?
2446
Get selected text from a drop-down list (select box) using jQuery
Hot Network Questions more hot questions
Question feed
Subscribe to RSS
Question feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-js
Stack Overflow
Products
Company
Stack Exchange Network
Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2022.5.20.42186
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.