admin管理员组

文章数量:1400503

I want to loop through a set of <a>'s in an unordered list.

In jQuery, I'd do it this way:

$("#list ul li a").each(function (x) {
 // Do Stuff
});

It's probably a simple question, but how do I pass a variable through as that id shown in the function there? I'd imagine it is something like:

  var myVar = 'foo';
   $("#list ul li a").each(myVar, function (x) {
     // Do Stuff
     console.log(x);
    });

Console out: foo.

But that doesnt seem to work, how do I do this?

I want to loop through a set of <a>'s in an unordered list.

In jQuery, I'd do it this way:

$("#list ul li a").each(function (x) {
 // Do Stuff
});

It's probably a simple question, but how do I pass a variable through as that id shown in the function there? I'd imagine it is something like:

  var myVar = 'foo';
   $("#list ul li a").each(myVar, function (x) {
     // Do Stuff
     console.log(x);
    });

Console out: foo.

But that doesnt seem to work, how do I do this?

Share Improve this question edited Mar 24, 2015 at 13:44 Chud37 asked Mar 24, 2015 at 13:42 Chud37Chud37 5,03714 gold badges68 silver badges129 bronze badges 5
  • $(this).attr('id'); will refer to the current element's id, so maybe this can be tha answer to your problem if i get your question correct. – Balázs Varga Commented Mar 24, 2015 at 13:43
  • 4 It's not clear what you mean by "pass a variable through". Code in the callback you pass to .each() can "see" local variables like "myVar" in the surrounding code. What is it that you want to do exactly? – Pointy Commented Mar 24, 2015 at 13:43
  • @Pointy Can it really? I've had numerous times when .each and .ajax cannot see my local variables. – Chud37 Commented Mar 24, 2015 at 13:44
  • 2 @Chud37 it depends on where they are declared. In your example, console.log(myVar) in the each() would work fine, though. – Rory McCrossan Commented Mar 24, 2015 at 13:46
  • @Chud37 Take a look at this: JavaScript Scope And Closures explained – Praxis Ashelin Commented Mar 24, 2015 at 13:49
Add a ment  | 

3 Answers 3

Reset to default 7

You don't need to pass variable on each(myVar, function(x). You can use myVar inside each with no problem.

var myVar = 'foo';
$("#list ul li a").each(function (x) {
   // Do Stuff
   console.log(x); // element index
   console.log(myVar); //foo
});

Let's see what is wrong with this code.

var myVar = 'foo';
$("#list ul li a").each(myVar, function (x) {
    // Do Stuff
    console.log(x);
});

You will get:

Uncaught TypeError: undefined is not a function

Because exects that first parameter to be a function and it's a variable.

Also function(x) will never be called because each take only 1 parameter.

Here is the solution,

just use "this" keyword like this:

        var myVar = 'foo';
        $("#list ul li a").each(function () {
           console.log($(this).attr('id'));
        });

Thanks

var myVar = 'foo';
   $("#list ul li a").each(myVar, function (id) {
     // Do Stuff
     console.log(id.attr("id"));
    });

本文标签: javascriptjquery each function and passing variablesStack Overflow