Friday, October 5, 2012

jQuery parent selector


The jQuery parent selector is used to get the parent element of a specific element.

.parent()

In this post we shall see on how to use the jQuery parent selector to get the parent element of a specific element.

The following script gets the parent element of the td element tdName.

    alert('Hierarchy Child:' + $("#tdName").attr('id'));
    alert('Hierarchy Parent:' + $("#tdName").parent().attr('id'));

The following script gets the parent element of the td element tdName on click of the btnHierarchySelector button.

$('#btnHierarchySelector).click(function(event) {
    event.preventDefault();               
    //
    // Hierarchy - Parent
    alert('Hierarchy Child:' + $("#tdName").attr('id'));

Search Flipkart Products:
Flipkart.com

No comments: