Check if checkbox is checked

Check if a checkbox is checked or unchecked.


Libraries

Code Snippet
// Pure JavaScript
if(document.getElementById('myCheckbox').checked) {
    // Checked
} else {
    // Unchecked
}


// jQuery
if($("#myCheckbox").is(':checked')) {
    // Checked  
} else {
    // Unchecked
}


// Assuming an event handler on a checkbox
if (this.checked) {
    // Checked
} else {
    // Unchecked
}


Implementation

The first two ways (pure JavaScript and jQuery) should work on any position with your scripts. The last way does only work in the event handler of a checkbox.





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 1 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    Checkbox
    Checked
    Unchecked
    jQuery



    Informations

    posted on 2016-09-13 21:17:55

    viewed 79 times

    snippet's UUID is 57d850e341d24

    Similar Snippets