jquery - Can't select direct parent element -
jquery - Can't select direct parent element -
i have html page globally exists of div
, section
, , ul
elements (of course of study more, common)
now in 4th list on page (e.g. below in bold)
<body> <div> <header> <ul id="firstlist"> <li> [..] <section> <ul id="secondlist"> [..] <section> <ul id="thirdlist"> [..] <section> <ul id="fourthlist"> <li><a>test 1</a></li> <li><a>test 2</a></li> <li><a>test 3</a></li> </ul> [..]
the thing that, when clicks on test, want loop through list-items position of click in list (it there items before or after)
herefore have following
$("a").click(function(event){ [..] var parentel = $(this).parents('ul').filter(":first").tagname; alert($(parentel).attr("id")); });
but gives me firstlist
output. assume jquery looks through document top down, output filter(:first)
(oh , get(0)
) makes sence guess.. how going first parent of clicked link?
if click on test 2
want fourthlist
output (and position, that's later)
try this
$("a").click(function(event){ [..] var $parentel = $(this).closest('ul'); alert( $parentel.attr("id") ); });
and position of link among it's siblings
$("a").click(function(event){ [..] // returns 0-based index of `li` among it's sibling `li`s var idx = $(this).parent().prevall().length; });
jquery
Comments
Post a Comment