jQuery Misc each() Method



The each() method in jQuery is used to execute a function for each matched element.

Syntax

The syntax is as follows −

$(selector).each(function(index,ele))

Above, the index is the position of the selector, whereas ele is the current element.

Let us now see an example to implement the jQuery each() method −

Example

 Live Demo

<!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(){          $("li").each(function(){             alert($(this).text())          });       });    }); </script> </head> <body> <h2>Devices</h2> <ol> <li>TV</li> <li>Laptop</li> <li>Headphone</li> <li>Earphone</li> <li>SSD</li> </ol> <button>Get each element</button> </body> </html>

Output

This will produce the following output −

Click “Get each element” to fetch each element one by one in the alert box −

On clicking “OK”, the next li would be visible −

In the same way, all other li elements would be visible.

Updated on: 2019-12-31T09:29:42+05:30

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements