each() method in jQuery


The each() method in jQuery specifies a fnction it's runs for each matched elements.

Syntax
$("li").each(function(){
  //...
});

Example

The following example shows, how to get inner text of each <li> element using jQuery each() method.

Demo

 
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>   
  </head>
  <body>
    <ul>
      <li>JS</li>
      <li>jQuery</li>
      <li>PHP</li>
    </ul>
    <input type='button' id='btn' value='Click'> 
  <script>
    $(document).ready(function(){
      $("#btn").click(function(){
        $("li").each(function(){
          var txt=$(this).text();
          alert(txt);
        });
      });
    });
  </script>
  </body>
</html>
Try it Yourself