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,186
  • 8
  • 48
  • 77

2 Answers2

27

Simple:

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

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

karim79
  • 334,458
  • 66
  • 409
  • 405
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 Wizard Says No More War
  • 64,101
  • 26
  • 136
  • 201