jQuery - Find top parent before div (parent of a parent of a parent...) -
my question title bit vague, couldn't think of less confusing, sorry!
here's want:
- find first anchor inside top level div.
- find top parent of anchor before above div.
- sorry if still confusing.
example:
<div id="container"> <div id="thisshouldbeignored"></div> <div id="ignorethistoo"></div> <div id="one"> <div id="two"> <div id="three"> <a href="" class="anchor">hello</a> </div> </div> </div> </div>
what want returned here div id of one
.
here's i'm doing, i'm using .html example.
var firstanchor = $('#container a')[0], parentid = $(firstanchor).parent().attr('id'); $(firstanchor).html(parentid);
this returns three
expect.
the next step i'm struggling. how continue parent until hit top level div, in example container
.
in short, want target first anchor inside container
, top level parent before container
one
.
any massively appreciated!
here's fiddle of have far: http://jsfiddle.net/uk6oocs7/
this should it:
$('#container div').has('a').first()
this select divs children of #container have child anchor element, , reduce first match.
Comments
Post a Comment