29er 29er - 1 year ago 129
Javascript Question

JQuery - getting the parent div from a nested element ID

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?

<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>

Answer Source

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


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download