Solved

jquery each

Posted on 2011-09-18
5
161 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
[X]
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
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
JScript Error 2 55
Problem to run jar file 4 155
Ecommerce: External Script Audit 6 82
Converting dates in JavaScript 2 82
The following article is comprised of the pearls we have garnered deploying virtualization solutions since Virtual Server 2005 and subsequent 2008 RTM+ Hyper-V in standalone and clustered environments.
Check out this step-by-step guide for asking an anonymous question on Experts Exchange.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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