---
title: "What is the use of jquery .each() function?"  
description: "What is the use of jquery .each() function?"  
author: "Manish Kumar"  
published: 2017-05-16  
updated: 2020-09-23  
canonical: https://www.mindstick.com/interview/23247/what-is-the-use-of-jquery-each-function  
category: "c#"  
tags: ["jquery"]  
reading_time: 2 minutes  

---

# What is the use of jquery .each() function?

Basically the jQuery each() function is used to loop through each element of the targer jQuery object.Very useful for multi element DOM manipulation looping arrays and object properities

**In this example alert box will be open 3 time**

```
  <script>    $(document).ready(function(){        $("button").click(function)        {            $("li").each(function)            {                alert($(this).text));            });        });    });    </script><ul>                <li>Emp1</li>                 <li>Emp2</li>                 <li>Emp3</li>            </ul>
```

## Answers

### Answer by Sarahh K.

The jQuery each method makes looping simpler. You can loop with the help of jQuery Each method anything like arrays, selector elements, etc.

The syntax of jQuery each method is:

`$(selector).each(``function``(index,element))`

`Suppose you have an array, let me show you how to loop with`

`jQuery each method:`

`var names = ["a1","a2","a3","a4","a5"];``$.each(names ,``function` `(index, value){````console.log(``"Index is: "``+ index +``" :: Value is: "` `+ value);`});

`For looping through selector elements with jQuery each:`

`$(``"div"``).each(``function``(index,value) {````//looping all div elements`

``

`}`

Here I am looping through all the div elements of the DOM.

You you remove the div and give a class name and in that way you loop through every element having that class.

`$(``".myclass"``).each(``function``(index,value) {````//looping all elements of that class`

``

`}`

\

### Answer by Manish Kumar

Basically the jQuery each() function is used to loop through each element of the targer jQuery object.Very useful for multi element DOM manipulation looping arrays and object properities

**In this example alert box will be open 3 time**

```
  <script>    $(document).ready(function(){        $("button").click(function)        {            $("li").each(function)            {                alert($(this).text));            });        });    });    </script><ul>                <li>Emp1</li>                 <li>Emp2</li>                 <li>Emp3</li>            </ul>
```


---

Original Source: https://www.mindstick.com/interview/23247/what-is-the-use-of-jquery-each-function

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
