Reputation: 93
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.
Upvotes: 2
Views: 67
Reputation: 122008
Since you tagged jquery ,You can loop through the class
elements like
$('.yourClass').each(function() {
alert(this.id);
});
Upvotes: 3
Reputation: 3955
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
});
Upvotes: 0