jQuery get() by index

Introduction

Get the name and value of the first <p> element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    var x = $("p").get(0);
    $("div").text(x.nodeName + ": " + x.innerHTML);
  });/*from  w  w  w  .  ja v a2s  . c o m*/
});
</script>
</head>
<body>

<p>This is a paragraph</p>

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

<div></div>

</body>
</html>

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

$(selector).get(index)
Parameter Optional Description
index Optional.which of the matching elements to get by index number



PreviousNext

Related