How to get the switch toggle state(true/false) in javascript?

How to get the switch toggle state(true/false) in javascript
var switchStatus = false;
$("#togBtn").on('change', function() {
    if ($(this).is(':checked')) {
        switchStatus = $(this).is(':checked');
        alert(switchStatus);// To verify
    }
    else {
       switchStatus = $(this).is(':checked');
       alert(switchStatus);// To verify
    }
}); 
Here’s How to add the text “ON” and “OFF” to toggle button
$("#togBtn").on('change', function() {
        if ($(this).is(':checked')) {
            $(this).attr('value', 'on');
            alert($(this).val());
        }
        else {
           $(this).attr('value', 'off');
           alert($(this).val());
        }
    }); 

Share this post:

Amitpal Singh
Amitpal Singh
Freelance website and application developer. Working from Sangrur Punjab in India.

How to Attribute?

Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
for Example: Website, Social Media, Blogs, ebooks , newsletter, etc.
Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
Copied!

Got a Question? Check out our FAQ Section.

Your action, our appreciation

It encourage us to give you more valuable content on website.