:only-of-typeS2C Home « Selectors « :only-of-type

Only of type child selector.

Shorthand version $(':only-of-type')

Description

The (':only-of-type') selector, selects elements that have no siblings with the same element name.

  • Only unique child elements will be selected.

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-of-type')Only of type selector match

Parameters

None.

Return

N/A.

:only-of-type Examplego to top of page Top

Selects all elements that have no siblings with the same element name.

The following example will check for single 'a' child elements within the main content area and turn the background colour yellow.


$(function(){
  $('#btn31').on('click', function() {
    $("#main a:only-of-type").css('backgroundColor', 'yellow'); 
  });
}); 

Press the button below to action the above code:


go to home page Homepage go to top of page Top