jQuery | Misc get() Method

The get() Method in jQuery is used to get the DOM element specified by the selectors.

Syntax

$(selector).get(index)

Parameters: This method accepts single parameter index which is optional. It is used to specify which of the matching elements to get by its index number.

Example 1: This example uses get() method to get the element specified by selector.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head
    <title>
        jQuery Misc get() Method
    </title>
      
    <script src=
    </script>
</head
  
<body style="text-align:center;">
            
    <h1 style = "color:green;" >  
        GeeksforGeeks
    </h1>  
      
    <h2>jQuery Misc get() Method</h2>
      
    <button>Click</button>
  
    <div></div>
      
    <script>
        $(document).ready(function() {
            $("button").click(function() {
                var x = $("h1").get(0);
                $("div").text(x.nodeName + ": " + x.innerHTML);
            });
        });
    </script
</body>
  
</html>  

chevron_right


Output:
Before Click on the button:

After Click on the button:

Example 2: This example use get() method to get the element specified by selector.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head
    <title>
        jQuery Misc get() Method
    </title>
      
    <script src=
    </script>
</head
  
<body style="text-align:center;">
  
    <h1 style = "color:green;" >  
        GeeksForGeeks
    </h1>  
      
    <h2>jQuery Misc get() Method</h2>
  
    <button>Click</button>
      
    <!-- Script to use get() method -->
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                var x = $("h1").get(0);
                alert(x.nodeName + ": " + x.innerHTML);
            });
        });
    </script
</body>
  
</html>  

chevron_right


Output:
Before Click on the button:

After Click on the button:



My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.