Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two jquery functions at the same time

I'm having two jquery's that I run and I want to combine them both in the same line. Here's an example of what i'm trying to do.

    ​$(function(){
    $('input').each(function(){
        if ($(this).is(':checked')) {
           $(this).after('this is checked');
        }
    });
    $('input').click(function(){
        if ($(this).is(':checked')) {
           $(this).after('this is checked');
        }
    });
});​

Now, both functions do the same thing, one is executed for checking if there are any inputs being checked from the backend, the other one just responds to user clicks.

I was thinking if i can combine them in a statement like this,

$('input').bind('each click');

but i noticed that even this wouldn't work with each. Any ideas?

Thanks!

like image 487
Joe Saad Avatar asked Feb 14 '26 04:02

Joe Saad


1 Answers

Just name the function..

function handler() {
    if ($(this).is(':checked')) {
       $(this).after('this is checked');
    }
}

then use it in both situations:

$(function(){
    $('input').each(handler);
    $('input').click(handler);
});​
like image 175
calebds Avatar answered Feb 15 '26 19:02

calebds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!