Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

jquery each

Posted on 2011-09-18
5
160 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
change car movement from jumping to swapping 8 101
how to avoid duplicate value selection in drop down 18 1,504
HTML coding help 6 71
Jquery height() issue 13 158
Most MSPs worth their salt are already offering cybersecurity to their customers. But cybersecurity as a service is wide encompassing and can mean many things.  So where are MSPs falling in this spectrum?
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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