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:

  1. find first anchor inside top level div.
  2. find top parent of anchor before above div.
  3. 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() 

jsfiddle example

this select divs children of #container have child anchor element, , reduce first match.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -