Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery fire function on all class elements

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

like image 626
Peter Avatar asked Dec 15 '11 13:12

Peter


2 Answers

Simple:

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

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

like image 123
karim79 Avatar answered Nov 04 '22 08:11

karim79


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?

like image 6
Shadow Wizard Hates Omicron Avatar answered Nov 04 '22 08:11

Shadow Wizard Hates Omicron