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开发者_StackOverflow you!
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).
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.
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.
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.....
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/
Using JQuery to get the child element, and standard JS to address the parent:
element = $('#childID')[0].parentNode
精彩评论