English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery :first-child Selector

jQuery Selectors

:first-child selector to select all elements that are the first child of their parent.

Using:last-childSelector to select the element that is the last child of its parent.

Syntax:

$(":first-child)

Example

Select each <p> element that is the first child of its parent:

$(document).ready(function(){
  $("p:first-child").css("background", "coral");
});
Test and See‹/›

Select the first <p> element within all <div> elements:

$(document).ready(function(){
  $("div p:first-child").css("background", "coral");
});
Test and See‹/›

jQuery Selectors