Friday, October 5, 2012

jQuery nth-child selector


The jQuery nth-child selector is used to get the nth child of a parent element.

nth-child(1)
In this post we shall see on how to use the jQuery nth-child
selector to get the nth child of a parent element

The following script gets the 1st and 2nd child elements of the parent tr element trName.

    alert('Hierarchy Parent:' + $("#trName").attr('id'));
    alert('Hierarchy 1st Child:' + $("#trName td:nth-child(1)").attr('id'));
    alert('Hierarchy 2st Child:' + $("#trName td:nth-child(2)").attr('id'));

The following script gets the 1st and 2nd child elements of the parent tr element trName on click of the btnHierarchySelector button.

$('#btnHierarchySelector).click(function(event) {
    event.preventDefault();               
    //
    // Hierarchy - nth Child
    alert('Hierarchy Parent:' + $("#trName").attr('id'));
    alert('Hierarchy 1st Child:' + $("#trName td:nth-child(1)").attr('id'));

Search Flipkart Products:
Flipkart.com

No comments: