jQuery first() method
As its name implies, the first() method returns the first element of the selected elements. If we need to get the last element, we can use the last() method.
Syntax
This method doesn’t accept any argument. The selector in the above syntax is mandatory to be specified.
Let’s see some of the illustrations to understand how to use the first() method.
Example1
In this example, we are using the first() method to get the first paragraph element from the set of elements. We are using the p as the selector, so the method will return the first paragraph element.
Output
After the execution of the above code, the output will be –
After clicking the given button, the output will be –
Example2
In this example, we are using the first() method to get the first span element from the set of elements.
Output
After the execution of the above code, the output will be –
After clicking the given button, the output will be –