Say I have the following markup:
<div>
<h3><a href="">link I want to select</a></h3>
<div>
<h3><a href="">link</a></h3>
</div>
<h3><a href="">link I want to select</a></h3>
<h3><a href="">link I want to select</a></h3>
<a href="">link</a>
</div>
Assuming the following...
...is there a clever way to select 'first anchor tag, and all other anchor tags that are nested at that same level' so that it returns the 1st, 3rd, and 4th links?
Worse case, we need to go in and just add specific classes to our HTML, but it'd be great if this could be done via pure selectors.
Not a pure selector, but how about:
var depth = $('#myanchor').parents().length;
var all_at_depth = $("a").filter( function() { $(this).parents().length == depth; } );
You can use parentsUntil() to get the distance between #myanchor and a specific parent, if that helps.
Check out siblings() and :first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With