Thursday, October 4, 2012

jQuery only-child selector


The jQuery only-child selector is used to filter out the elements in a page which are the only child for their immediate parent. 

$("input:only-child ")

In this post we shall see on how to use the jQuery only-child selector to filter out all the elements in a page which are the only child for their parent elements.

The following script filters out all the elements in a page, which are the only child for their parent.

    $("input:only-child").each(function() {
        alert('Hierarchy - Only child:' + $(this).attr('id'));
    });

The following script filters out the elements in a page which are the only child for their parent on click of the btnHierarchySelector button.

$('#btnHierarchySelector).click(function(event) {
    event.preventDefault();               
    //
    // Hierarchy - Only child
    $("input:only-child").each(function() {
        alert('Hierarchy - Only child:' + $(this).attr('id'));

Search Flipkart Products:
Flipkart.com

No comments: