Solved

jquery each

Posted on 2011-09-18
5
158 Views
Last Modified: 2012-05-12
http://api.jquery.com/each/

please take a look at example2, what's the difference between these two methods?
<script>
     $("span").click(function () {
      $("li").each(function(){
        $(this).toggleClass("example");
      });
    });
 
	 $("span").click(function () {
      
        $('li').toggleClass("example");
      
    });
	
	
</script>

Open in new window

0
Comment
Question by:charmingduck
5 Comments
 
LVL 13

Accepted Solution

by:
dsmile earned 84 total points
ID: 36558134
.each( function(index, Element) ) Returns: jQuery

Description: Iterate over a jQuery object, executing a function for each matched element.


This code
$("span").click(function () {
      $("li").each(function(){
        $(this).toggleClass("example");
      });
    });

Open in new window


Will do toggleClass to each of "li" found in "span" by looping throuhg the "li" list.
This means you can do something else with each of these "li" individually.

While this code
$("span").click(function () {
      
        $('li').toggleClass("example");
      
    });

Open in new window

will do toggleClass for ALL the "li" found
0
 

Author Comment

by:charmingduck
ID: 36558147
sorry, by

$("span").click(function () {
     
        $('li').toggleClass("example");
     
    });

I actually meant
$("span").click(function () {
     
        $('span li').toggleClass("example");
     
    });



and when you say something else with each of these "li" individually, do you mean like this? am I understanding this correctly? is this the major advantage or difference? or maybe can you show me an example that in some cases it'd be better to choose each over simply using the all ** ?

$("span").click(function () {
      $("li").each(function(){
        $(this).toggleClass("example");
       $(this).somethingelse();
      });
    });
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36558158
There is no difference as far as functionality is concerned. Both will toggle class 'Example' of li, in case any span is clicked
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 41 total points
ID: 36558165
You can't do something like that :
      $("li").each(function(){
          $(this).toggleClass("example");
          $(this).somethingelse();
          alert("ok");
          function1();
          function2();
      });
without a loop (using $.each or $.map... while, for, do...)
0
 
LVL 13

Assisted Solution

by:dsmile
dsmile earned 84 total points
ID: 36558169
@charmingduck:

first of all, your new code doesn't work

$("span").click(function () {
     
        $('span li').toggleClass("example");
     
    });


second, yes, that's what I mean, but specifically, it allows you to do this (do distinguish with ALL)

$("span").click(function () {
     
       $('li').each(function(index) {
    if (index % 2 == 0)
    {
      alert('you cant do this with second case'); // this message shows each 2 item
    }
  });
     
    });


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Note: This is the second blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   Every month t…
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now