All TalkersCode Topics

Follow TalkersCode On Social Media

jQuery Set Checkbox Checked Based On Value

Last Updated : Jan 1, 2023

jQuery Set Checkbox Checked Based On Value

In this article we will show you the solution of jQuery set checkbox checked based on value, here we needs to use prop() method and checked property for achieve the result the prop() method sets or returns properties and values of the selected elements.

This method returns property value then it returns value of first matched element and ‘checked’ property returns state of checkbox as result.

Step By Step Guide On jQuery Set Checkbox Checked Based On Value :-

Here we defined html block with input element ‘check me’ which contains ‘type, id’ attributes with respected values ‘checkbox, ch’ and we sets it checked state based on value of checked state.

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 we defined if condition it will checks checkbox ‘ch’ checked property value using prop() method whether in unchecked state then we sets checkbox checked state as ‘true’ for sets checkbox ‘ch’ in checked state.

<!DOCTYPE html>
<html>
    <head>
        <title>Set checkbox as checked by value</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script>
            $(document).ready(function (){
                if($('#ch').prop("checked",false)){
                    $('#ch').prop("checked",true);
                }
            });
        </script>
        </head>
        <body>
                <input type="checkbox" id="ch">check me
        </body>
</html>
  1. <!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.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. 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.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. 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.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we specified if condition there we checking whether checkbox ‘ch’ is unchecked by using prop() method. If return true means then checkbox in unchecked state because as we know any checkbox at its initial stage it will in uncheck state.
  8. The ‘$('#ch').prop("checked",false)’ line refers checkbox value of checked state by using this we changing it state to ‘true’.
  9. Then we sets it’s checked state as ‘true’ for sets checkbox ‘ch’ in checked state by prop() method.
  10. In html block we defined input element ‘check me’ checkbox with id ‘ch’.
  11. 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 set checkbox in checked 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 ‘check me’ in checked state.

I hope this article on jQuery set checkbox checked based on value helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials