jQuery Selectors: Filter out everything but the first element

This entry is part 28 of 33 in the series jQuery Selectors


We will use the example $(‘p:first’) to highlight the first <p> element.

<!DOCTYPE html>
<head lang='en'>
    <meta charset='UTF-8'>
    <title>Codecrawl.com - HTML5</title>
    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>
        $(function () {
            $('p:first').css('background-color', 'yellow');


        <p>Ushuaia is the capital of Tierra del Fuego in Argentina</p>
        <p>It is commonly regarded as the southernmost city in the world.</p>

        <p>They also built a railway now a tourist attraction known as the End of the World Train</p>


We can also select the last <p> element using $(‘p:last’) illustrated by the link button below.

Series Navigation<< jQuery Selectors: Filters out all odd indexed elements
jQuery Selectors: Selects elements that are of header type >>

Leave a comment

Leave a Reply