15

I want to execute some function on all class elements. How can i do that?

what i want is something like:

// i want to fire my function on all class elements
$('.myClass').go(function(){
    // my function using "this" object
});

i know this is a dumb question, i used to work on propotype for some time and now i don't remember proper function for jquery

Peter
  • 16,453
  • 8
  • 51
  • 77

2 Answers2

27

Simple:

$(".myClass").each(function() {
    ...
});

http://api.jquery.com/each/

karim79
  • 339,989
  • 67
  • 413
  • 406
6

Just use the common .each() iterator:

$('.myClass').each(function() {
    go($(this));
});

Another option if you're into more elegant code, write your own plugin - good answer can be found in this question: How to create a jQuery plugin with methods?

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208