select - jQuery: Selecting a link one level up from a lower level link -
i'm working unordered list of links has 2 levels:
<ul class="level-1"> <li class="item-a"><a href="#">a</a></li> <li class="item-b"><a href="#">b</a> <ul class="level-2"> <li class="item-1"><a href="#">1</a></li> <li class="item-2"><a href="#">2</a></li> <li class="item-3"><a href="#">3</a></li> </ul> </li> <li class="item-c"><a href="#">c</a></li> </ul>
i click 1 of .level-2 links, , want select higher level link list contains clicked link. (for example, click either 1, 2, or 3 - , want select b.)
if clicked link selected $(this).find('a')
-- how can select higher level link?
would appreciate advice!
one way go closest <ul>
, again closest <li>
, , down <a>
:
$('a').click(function() { var $a = $(this).closest('ul').closest('li').find('a:first'); // $a <a> want. });
this approach flexible , not overly dependent on dom structure, you'd able add things before or after <a>
want without breaking code (unless of course started adding <li>
s or <ul>
s).
there other ways above pretty simple , straight forward , should second concern (your first being works).
referenences:
Comments
Post a Comment