:first-of-type
S2C Home « Selectors « :first-of-type
First of child type selector.
Shorthand version $(':first-of-type')
Description
The :first-of-type
selector, selects the first element types that have no other element with both the same parent and the same element name coming before it in the document tree.
- 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 examples below to narrow the search and improve performance.
Syntax
Signature | Description |
---|---|
jQuery(':first-of-type') | First of child type selector match |
Parameters
None.
Return
N/A.
:first-of-type
ExamplesTop
Selects all elements that are the first among siblings of the same element name.
The following example will check for the first 'h2' element within the document and turn the background colour grey (top of left sidebar).
$(function(){
$('#btn1').on('click', function() {
$("#sidebarleft h2:first-of-type").css('backgroundColor', 'grey');
});
});
The following example will check for the first 'form' element within the document and turn the background colour orange (above form).
$(function(){ $('#btn2').on('click', function() { $("form:first-of-type").css('backgroundColor', 'orange'); }); });
The following example will check for the first 'h2' element within the document and turn the background colour yellow (first preformatted code above).
$(function(){
$('#btn3').on('click', function() {
$("pre:first-of-type").css('backgroundColor', 'silver');
});
});