:only-childS2C Home « Selectors « :only-child

Only child selector.

Shorthand version $(':only-child')

Description

The (':only-child') selector, selects all elements that are the only child of the specified parent.

If this selector is not preceded by another selector the universal selector ("*") is implied and so the whole DOM will be searched. Use another selector as in the example below to narrow the search and improve performance.

Syntax


Signature Description
jQuery(':only-child')Only child match

Parameters

None.

Return

N/A.

:only-child Examplego to top of page Top

Selects all elements that are the only child of the specified parent.

Nodes with siblings will not be selected.

The following example will select the 'a' elements that are the only children of 'p' elements and change the background colour of these elements to yellow.


$(function(){
  $('#btn28').on('click', function() {
    $('p a:only-child').css('backgroundColor', 'yellow');
  });
}); 

Press the button below to action the above code:


go to home page Homepage go to top of page Top