Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming jQuery Functions

$(".song").live('click', function songClick() {
    //do stuff
});

Can you name a function like above and then call it at a later point? I tried, but it didn't work. Do I have to pass an event to it?

like image 697
switz Avatar asked Mar 27 '11 19:03

switz


1 Answers

Note for reference's sake:

The live method was removed in version 9. Anyone coming across this question now should use on() instead. But otherwise, should work the same as above answers.

function songClick() {
//code
}

$(".song").on('Click', songClick);


If you only want the event handler to run once you can also use one() as well.

like image 154
K.S. Avatar answered Nov 15 '22 16:11

K.S.