jQuery .get() method get element by index.

Description

.get() accepts a zero-based index as its argument and returns the element at that index.

Get list element by index

The following code shows how to get the list element by index.


<!DOCTYPE html>
<html>
    <head>
        <script src="http://java2s.com/style/jquery-1.8.0.min.js">
        </script>
        <script>
            $(function(){<!--from  w  ww.j  a  v a2 s  .  co  m-->
                var listElements = $("li");
                var secondEl = listElements.get(2);
                var fourthEl = listElements.get(3);
                document.writeln(secondEl.innerHTML + " and " + fourthEl.innerHTML);
            });
        </script>
    </head>
    <body>
        <ul>
            <li>A</li>
            <li>B</li>
            <li>C</li>
            <li>D</li>
            <li>E</li>
            <li>F</li>
        </ul>
    </body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities