Why is my call to $ .each returning more results than there are elements?

I am having trouble simply iterating through a div from a specific class.

There are two divs, but I get 14 iterations.

  $(function() {
      $.each("div.container", function(){
         alert( "test" );
      });
   });

and html

<div id="div1" class="container">

</div>

<div id="div2" class="container">

</div>

Can someone explain what I'm doing wrong? thanks in advance

+3
source share
5 answers

try it

$(function() {
      $("div.container").each( function(){
         alert( "test" );
      });
   });
+7
source

You should use the following:

$(function() {
    $("div.container").each( function(){
        alert( "test" );
    });
});

There is a big difference between $. each and .each .

The first method is a generic iterator function that is used to iterate over arrays and objects, where the last method is used to cycle through a set of jQuery objects.

$.each('div.container'), , . div.container 13 (, 13) , 13 (. ).

+6
 $(function() {
      $.each($("div.container"), function(){
         alert( "test" );
      });
 });
0

If you want to use $ .each ()

You should put jquery objects in parameter, but not selector text

      $(function() {
      $.each($("div.container"), function(){
         alert( "test" );
      });
   });
0
source
$('div.container').each(function () {
    alert('test');
});

That's all you need, ha ha .; D

0
source

All Articles