.first()
Reduce the set of matched elements to the first in the set.
.first()🡢 jQuery
Given a jQuery object that represents a set of DOM elements, the .first()
method constructs a new jQuery object from the first element in that set.
Consider a page with a simple list on it:
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
We can apply this method to the set of list items:
$("li").first().css("background-color", "red");
The result of this call is a red background for the first item.
Highlight the first item in a list.
JS
<ul>
<li>Look:</li>
<li>This is some text in a list.</li>
<li>This is a note about it.</li>
<li>This is another note about it.</li>
</ul>
CSS
.highlight {
background-color: yellow;
}
HTML
$("ul li").first().addClass("highlight");
DEMO