4

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?

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. 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? 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. 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. Commented Mar 24, 2015 at 13:46
  • @Chud37 Take a look at this: JavaScript Scope And Closures explained Commented Mar 24, 2015 at 13:49

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I think I misunderstood the variable scope. This works, thank you!
-1

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

4 Comments

I don't think this answer is what the OP was looking for.
@TomerM this is not the answer I was looking for.
@Chud37 I were made adaptation to your needs.
@Chud37 myVar defined in function scope! you dont need to pass it.
-4
var myVar = 'foo';
   $("#list ul li a").each(myVar, function (id) {
     // Do Stuff
     console.log(id.attr("id"));
    });

1 Comment

.each() takes only one argument, and that argument must be a function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.