jQuery <input> is checked via :checked Selector

Introduction

Use the jQuery :checked selector to check the status of checkboxes.

The :checked selector is for radio button and checkboxes.

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<title>Check the Status of Checkboxes</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    $(document).ready(function(){
        $('input[type="checkbox"]').click(function(){
            if($(this).is(":checked")){
                $("#result").html("Checkbox is checked.");
            }//from w  w  w.  j av a2s .c o  m
            else if($(this).is(":not(:checked)")){
                $("#result").html("Checkbox is unchecked.");
            }
        });
    });
</script>
</head>
<body>
    <p><input type="checkbox"> Check or uncheck the checkbox to get the status.</p>
    <div id="result" style="background: yellow;"></div>
</body>
</html>



PreviousNext

Related