Search code examples
jqueryparent

jQuery get *exact* parent id


I'd usually do $('ChildID').parent().attr('id'), but that would only get the first div with that id.

<div id="FOO1">
    <div id="bar"></div>
    <div id="bar"></div>
    <div id="bar"></div>
</div>
<div id="FOO2">
    <div id="bar" onClick="alert($(this).parent().attr('id'));"></div>
</div>

That would alert FOO1, but what I need it to do, is alert the actual parent, not the parent of the first div with such an id.


Solution

  • It gives me correctly FOO2

    http://sandbox.phpcode.eu/g/cbb12.php