tags:

views:

615

answers:

5

Hi, i am trying to access an element by using an ID of one of it's nested elements, using JQuery. For example : I need to find 'myDiv' from its nested 'searchBox' element. The issue is that the hierarchy changes and the searchBox will not always be in the same nested position. so i essentially need something that says: Give me the DIV that contains the element with the ID of 'searchBox'.(see below) Any suggestions? Thanks!

<div id="myDiv" class="CommonGridView_ListBoxPopupPanel"> 
Some text here : 
<table><tbody><tr><td><input class="tboxw" type="text" id="btnFind"/></td>  
 <td>some content</td> </tr> 
 <tr> <td><textarea id="searchBox">text area content</textarea> </td> 
 </tr> 
 </tbody> 
</table> 
<table><tr><td></td></tr></table>
</div>
+2  A: 
$('#searchBox').parents('#myDiv')
chaos
-1 parent() will not work.
Wayne Khan
parents (notice the 's') will work
CMS
0 my bad. I must've missed out the "s".
Wayne Khan
A: 

You can use this:

var searchBox = $('#searchBox');

// Have the searchBox jQuery object, so find the ancestors that match a specific selector.
var parentDiv = searchBox.parents('div#myDiv');

The full documentation is available here.

Peter
btw, using nodeName#id is slower than just specifying the id
redsquare
@redsquare: It actually isn't, jQuery is optimized to forgive this mistake and only looks for the ID. (I asked John Resig himself about this)
Paolo Bergantino
I've tested it myself and can assure you it was. Will do a demo when I get to work.
redsquare
Paulo here you go http://pastebin.me/4a52e68d803a0
redsquare
+2  A: 

If you know the id, and remembering id's have to be unique, just grab the element like you normally would.

var $parentDiv = $('#searchBox');

There is no benefit from using .parents as the answers above show as it is just slower as it gets all the parents in the hierarchy and filters on them rather than using getElementById. If for instance you knew only the className then you would be best using .closest('div.class') as that steps each parent above one by one until a match is found.

The only time you would use .closest is if you wanted to make sure that searchBox was indeed a parent of your element.

if ( $element.closest('#searchBox').length ){
     //do something
}
redsquare
+1 for pointing the right information out. :)
Paolo Bergantino
Asked John Resig again about the tag#id stuff, he said it does work as expected. The reason there is more calls going on is because it has to check the nodeName too, so that you could theoretically do span#myid and div#myid (not at the same time) - I still don't see why it is so many more calls, but he says it's fine so I'm inclined to believe him.
Paolo Bergantino
But what if I don't know the ID of the DIV, only the child element ?
29er
you can use any css selector does not have to be an id .closest('div.someclass') etc
redsquare
+1  A: 

If you want to find the wrapping DIV element (regardless of the ID on that DIV) then you'll want this jQuery selector:

$("#searchBox").closest("div");

The closest([selector]) function will find the wrapping element that matches the selector.

Jeff Meatball Yang
"The closest([selector]) function will find all the wrapping elements that match the selector." - no, it will only find the closest one. closest will never return > 1 elements.
Paolo Bergantino
Thanks for the clarification Paolo - I was mistaken about closest and edited my answer.
Jeff Meatball Yang
It's okay, I actually misspoke too: closest can return > 1 if the $(selector) part of it returns more than 1 element (it would return the closest of each of the selector elements)
Paolo Bergantino
thanks very much. this worked best for me because i don't always know what the ID of the parent DIV will be.
29er
+1  A: 

You can use

$('#searchBox').parents('div:first')

or

$('#searchBox').parents('div:eq(0)')
kara