Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a better way to check if a checkbox is defined?

currently i am using

var email, fax, sms = false;  
    if($('#uemail:checked').val() != undefined)  
        email = true;  
    if($('#ufax:checked').val() != undefined)  
        fax = true;  
    if($('#usms:checked').val() != undefined)  
        sms = true;  

but its such a long way to write it.

is there a better way to write this?

like image 510
Hailwood Avatar asked Jan 20 '26 14:01

Hailwood


1 Answers

Try this:

if($('#uemail').is(':checked'))
    email = true;

Or even shorter:

email = $('#uemail').is(':checked');

You're passing the :checked selector into jQuery's .is() method which returns a boolean value;

  • http://api.jquery.com/is/
like image 114
user113716 Avatar answered Jan 22 '26 04:01

user113716



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!