2

Ok, so I have a class applied to a group of images. I am trying to create a for loop that will use the elements of a class for the for loop. I know in python you can go "for element in thing" and was wondering if there was something similar in Javascript. I found some information online about the javascript for/in loop and have the following code written:

    function walkingFeet(){
        for (foot in *class named walkingFoot*){
            [code to be executed]
        }
    }

I basically am just having trouble finding out what to put where the asterisks are and if that sort of syntax is something that I can even do with Javascript. Any help would be greatly appreciated.

ThatOneGuy
  • 93
  • 1
  • 10
  • 2
    With jQuery you'd only use `.each` anyway, but notice that the `for in`-loop is only designated for object property enumeration. On arrays (and array-like objects like NodeLists or jQuery collections) you should use standard `for` iteration (with a counter up to `.length`). See http://stackoverflow.com/questions/500504/ – Bergi Aug 16 '13 at 14:55

2 Answers2

3

Since you tagged jquery ,You can loop through the class elements like

$('.yourClass').each(function() {
     alert(this.id);  
});

Learn more here on each()

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

You tagged this question with jQuery, so I assume you have access to it at this point of the script: It's easiest to be done with jQuery's each-function:

$('.element').each(function(index, element) {
    // index = the current index, the first would be 0
    // element = the html-dom-element, the same as 'this'

    // code to be executed
});
nirazul
  • 3,928
  • 4
  • 26
  • 46