Javascript examples for jQuery Method and Property:get
The get() method gets the DOM elements by the selector.
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:
<!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>