All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Check If Checkbox Is Checked In jQuery

Last Updated : Mar 11, 2024

How To Check If Checkbox Is Checked In jQuery

In this article we will show you the solution of how to check if checkbox is checked in jQuery, JavaScript has checked the checkbox. HTML input types known as checkboxes serve as selection boxes. Each radio button within a single group of radio buttons allows users to select just one choice.

The checkbox for the same group still allows for the selection of numerous values.

There are other advantages to using checkboxes.HTML can be used to add a checkbox to a webpage, but JavaScript is required to give the checkbox action.

Depending on whether a checkbox is selected, checkboxes may behave differently in software.

There are two ways provided by jQuery to determine if a checkbox is selected not: prop() and is().

In contrast to radio buttons, which can only be used to select one option, checkbox input types allow users to select multiple options.

Lists can be selected from multiple options using this method. There are a few properties in every element that let Javascript control the element in a particular way.

A checkbox has the checked property as one of its properties.

Step By Step Guide On How To Check If Checkbox Is Checked In jQuery :-

<!DOCTYPE html>
<html>
  <head>
    <title>TalkersCode title of Document</title>
    <script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>
  </head>
  <body>
    <input id="checkbox1" type="checkbox" name="first" value="1">
    <input id="checkbox2" type="checkbox" name="second" value="2">
    <input id="checkbox3" type="checkbox" name="third" value="3">
    <script>
      $(document).ready(function() {
          $('input[type="checkbox"]').click(function() {
              if($(this).prop("checked") == true) {
                alert("Checkbox is checked.");
              }
              else if($(this).prop("checked") == false) {
                alert("Checkbox is unchecked.");
              }
            });
        });
    </script>
  </body>
</html>
  1. To begin, we need to write <HTML>, which tells the browser what version of HTML we're using. HTML documents contain tags as their first element.
  2. In the head tag, describe the heading of the project. There are no closed brackets in the title and final brackets, unlike the final brackets.
  3. The <script> tag was then added. The script tag also includes the javascript google API run or an explanation of the code or file we used.
  4. In addition to closing the script, the head is also closed.
  5. In this case, the body tag describes the body of the website. Basically, this is where you write the content for the website.
  6. Clicking the Submit button after selecting the Yes checkbox will result in a message.
  7. You will see an error message if you click the Submit button on a form where the checkboxes are unchecked.
  8. Our next step was to open the script.
  9. In order to declare an alert, we create an if statement and an else statement.
  10. Then we close </script> </body></html>

Conclusion :-

Checkboxes can be checked or unchecked easily using this method. As an input element, the checkbox is a type of HTML element. Checking or unchecking a particular option is done with it.

A checked property on checkboxes indicates that the box is checked. It is possible to tell whether a checkbox is ticked by how often it is checked. One illustration is the element.checked attribute.

I hope this article on how to check if checkbox is checked in jQuery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪