tags:

views:

30

answers:

1

Update: I'll have to go back to the drawing board on this one. Thanks for all the feedback.

Hi All,

Given simple DOM:

<form id="form1">
...
</form>

<div>
   <div>
      <a href="#" id="export">Export</a>
   </div>
</div>

Starting with id=export, is there a way to go up a variable amount of divs and then get the closest form id?

Thanks, rodchar

+1  A: 

EDIT: You can try this, assuming the form has no parents:

alert($(this).parents(":last").prevAll("form:first")[0].id);

I think you will need to show more relevant document structure, as others have pointed out.

karim79
*Get the first ancestor element that matches the selector* . But `form` is not an ancestor of `a`.
Felix Kling
It's not working for me since the top container div is a sibling of the form (I get undefined error)
rod
i think, that wouldn't help since `#export` is not a child element of `form`...
Sinan Y.
Oops, I missed that. I assumed the anchor was inside the form, my eyes, they lied to me.
karim79
@all - I need to work on the structure a little bit more. Seems that I don't have valid html to begin with. Sorry for any inconvenience. Thanks all for the feedback.
rod
haha i was getting lost anyway :P `$("a#export").parents("*:has(form)").eq(0).find("div:has(a#export)").prev("form").attr("id")`
meo