0 votes
344 views
in jQuery by
What is each() function in jQuery? How to use it? How to use in our project. Can you provide better  idea about it?

1 Answer

0 votes
by (4.4k points)

each():- The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the current element.

You can use each() method in your project with following type:

<div>
  <p>Hello</p>
  <p>Dear</p>
  <p>jQuery</p>
</div>

You select the items and iterate all of them:

$( "p" ).each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});

See the following out in log console:

0: Hello
1: Dear
2: jQuery

You can stop the loop from within the callback function by returning false.

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...