tags:

views:

240

answers:

6

Hi all,

I've got an element which resides inside a div. I'd like to get the div by the child's ID, is this possible? if so how?

Thank you!

+5  A: 

Try this:

$("#id").parent()

My first suggestion #id:parent doesn’t work as :parent selects all nodes that are parent of other nodes (including text nodes).

Gumbo
Ahh, the joys of Jquery. Learnt something, thanks :)
Pekka
A: 

Using JQuery to get the child element, and standard JS to address the parent:

element = $('#childID')[0].parentNode
Pekka
To use this method, you need the `[0]`: `$('#childID')[0].parentNode`
Doug Neiner
Cheers Doug, I'm still mixing up Prototype and JQuery in my head. :)
Pekka
+1  A: 

Use the parent method of jQuery:

$("#myChild").parent()

You can even give it a selector if the parent element you're looking for is not the direct parent:

$("#myChild").parent(".container")

There's a slew of functions that help you traverse the DOM, and you can find detailed information in the jQuery reference.

Shtééf
+1  A: 

Use jQuery to get the child element, and jQuery to address the parent

element = $('#childID').parent();

Note: Yes, this is similar to Pekka's answer. It was meant to be humorous in that regard. If you don't find it humorous.....

Chacha102
+1  A: 

If you are not looking for the immediate parent, you can also use:

$('#childID').closest("tr");

Where tr is a selector for the parent you're trying to find. This is useful if you aren't sure of the depth of the hierarchy.

ScottE
+1  A: 

I agree:

  $("#id").parent()

Is the best method. I had this issue the other day, but was quickly resolved. I realize this was already answered but I am new. And if I had been awake! pow pow! the answer would have flown out. I'd appreciate an upvote

BTW: here is a great new reference for jQuery http://jqapi.com/

Juuccy