All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Get Checkbox Value If Checked

Last Updated : Mar 11, 2024

jQuery Get Checkbox Value If Checked

In this article we will show you the solution of jQuery get checkbox value if checked, here we are going to show you example for get checked checkbox value 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’ so by using this we can get checked checkbox result.

Step By Step Guide On jQuery Get Checkbox Value If Checked :-

Here we defined html block with one input element ‘checkbox’ type with id, value 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 ‘sel’ so which is refers when clicks on ‘Click Here’ click() event will load particular process.

There we defined is() method with input element ‘s’ of id attribute in if condition which checks check box state whether checked or not then result will displayed on alert() method.

<!DOCTYPE html>
<html>
    <head>
        <title>Get Checked Checkbox Value</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("#sel").click(function(){
                    if($('#s').is(':checked')){
                    alert("Checkbox Value Is "+$("input[type='checkbox']").val());
                    }
                    else{
                    alert("Checked is not checked");
                    }
            });
        });
        </script>
    </head>
    <body>
        <input type="checkbox" id="s" value="Yes">Yes
        <button id="sel">Click Here</button>
    </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() and click() method within that we used is() method as we know which is used for check element state whether checked or not by specifying ‘:checked’ in this method which is appends with element ‘#s’ i.e refers input tag, if its not checked then else alert message will display.
  8. So which process will begin when user clicks on button ‘Click Here’ id ‘s’ after loading this program on browser.
  9. In html block we defined input element checkbox ‘Yes’ with id ‘s’, value ‘Yes’ and a button ‘Click Here’ with id attribute ‘sel’.
  10. 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 get value from of checkbox when checked on it 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 ‘Yes’ with button Click Here, user needs to clicks on it then alert message will appear with checked check box result as ‘Checkbox Value Is Yes’, otherwise we get result as ‘Checked is not checked’.

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