jQuery Check If Checkbox Is Checked
Last Updated : Mar 11, 2024
In this article we will show you the solution of jQuery check if checkbox is checked, here we are going to show you example for get checked checkbox state like whether checked or not with the help of is() method.
The is() method checks the current selection against an expression and returns true, if at least one element of the selection fits the given selector. If no element fits then it returns ‘false’.
Step By Step Guide On jQuery Check If Checkbox Is Checked :-
Here we defined html block with one input element ‘checkbox’ type with id attributes and a button ‘Click Here’ with id attribute.
In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that defined click() method with id ‘btn’ so which is refers when clicks on ‘Click Here’ click() event will load particular process.
There we defined is() method with input element ‘enable’ of id attribute in if condition which is checks checkbox state condition whether checked or not then alert message will displayed.
<!DOCTYPE html> <html> <head> <title>Checking Whether Checkbox is Checked</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("#btn").click(function(){ if($('#enable').is(':checked')){ alert("Checked is checked"); } else{ alert("Checked is not checked"); } }); }); </script> </head> <body> <input type="checkbox" id="enable">Enable <button id="btn">Click Here</button> </body> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Here we imported open source jquery library support file which is needed for coding using jquery in program.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- In script we defined ready() and click() method within that we used is() method as we know which is used for check checkbox state whether checked or not by specifying ‘:checked’ in this method which is appends with input element ‘#enable’, then respective alert message will display.
- So which process will begin when user clicks on button ‘Click Here’ id ‘btn’ after loading this on browser.
- In html block we defined input element checkbox ‘Enable’ with id ‘enable’ and a button ‘Click Here’ with id attribute ‘btn’.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
Conclusion :-
In conclusion now we are able to know how to check checkbox state using jquery.
Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.
When we executes program on browser we can see check box ‘Enable’ with button Click Here, user needs to clicks on it then alert message will appear as ‘Checked is checked’, otherwise we get result as ‘Checked is not checked’.
I hope this article on jQuery check if checkbox is checked helps you and the steps and method mentioned above are easy to follow and implement.