jQuery Selectors: Applying indexed selector to table row

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

$(‘tr:eq(4)’)

As in the earlier example, the index selector could be applied to selecting a particular row in a table.

<!DOCTYPE html>
<html>
<head lang='en'>
    <meta charset='UTF-8'>
    <title>HTML Table</title>
    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>
    <script>
        $(function () {
            $('tr:eq(4)').css('background-color', 'lightgray');
        });
    </script>
</head>
<table border='1'>
    <tr>
        <th>ID</th>
        <th>artiste</th>
        <th>album</th>
        <th>year</th>
    </tr>
    <tr>
        <td>1</td>
        <td>Rod Stewart</td>
        <td>Maggie May</td>
        <td>1973</td>
    </tr>
    <tr>
        <td>2</td>
        <td>New Order</td>
        <td>Substance</td>
        <td>1987</td>
    </tr>
    <tr>
        <td>3</td>
        <td>Nirvana</td>
        <td>Smell Like Teen Spirit</td>
        <td>1992</td>
    </tr>
    <tr>
        <td>4</td>
        <td>Rod Stewart</td>
        <td>Out Of Order</td>
        <td>1988</td>
    </tr>
</table>
</body>
</html>

jQuery Selectors: The greater then gt selector

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

$(‘:gt(index)’)

The greater then gt selector will filter the list to include elements after a specific zero-based index.

We use $(‘p:gt(1)’) as an example.

There are 3 <p> elements in the example, only the last <p> element will be highlighted with yellow background.

<!DOCTYPE html>
<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>
    <script>
        $(function () {
            $('p:gt(1)').css('background-color', 'yellow');
        });
    </script>
</head>
<body>

<article>
    <header>
        <h1>Ushuaia</h1>

        <p>Ushuaia is the capital of Tierra del Fuego in Argentina</p>
    </header>
    <section>
        <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>
    </section>
</article>

</body>
</html>

The less then lt will do the reverse of gt as shown by the link button below.

jQuery Selectors: Selecting an animated object

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

For jQuery, it is possible to select an animated object.

<!doctype html>
<html lang='en'>
<head>
    <meta charset='utf-8'>
    <title>animated demo</title>
    <style>
        div {
            background: yellow;
            border: 1px solid #AAA;
            width: 80px;
            height: 80px;
            margin: 0 5px;
            float: left;
        }

        div.colored {
            background: green;
        }
    </style>
    <script src='//code.jquery.com/jquery-1.10.2.js'></script>
</head>
<body>

<button id='run'>Run</button>

<div></div>
<div id='mover'></div>
<div></div>

<script>
    $('#run').click(function () {
        $('div:animated').toggleClass('colored');
    });

    function animateIt() {
        $('#mover').slideToggle('slow', animateIt);
    }

    animateIt();
</script>

</body>
</html>