jQuery first and last methods
There are also separate methods first
and last
to select the first and last element of a set.
Let's consider the work of the above methods using the following paragraphs as an example:
<p>text</p>
<p>text</p>
<p>text</p>
Let's find all the paragraphs, then select the first one from those found and assign it the text '!'
:
$('p').first().text('!');
Now, let's choose the last one from the found ones and give it the text '!'
:
$('p').last().text('!');
Find the first li
in the set and color it red.
Find the last li
in the set and color it green.