get() Method to get the element by index - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:get

Description

The get() method gets the DOM elements by the selector.

Syntax

Parameter Require Description
index Optional. the matching elements to get (by index number)

The following code shows how to get the name and value of the first <p> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        var x = $("p").get(0);
        $("div").text(x.nodeName + ": " + x.innerHTML);
    });/*  ww  w  .jav  a 2  s. c  om*/
});
</script>
</head>
<body>

<p>This is a paragraph</p>
<p>This is a paragraph 2</p>
<p>This is a paragraph 3</p>
<p>This is a paragraph 4</p>

<button>Get the p DOM element</button>

<div></div>

</body>
</html>

Related Tutorials