:checked Selector in jQuery for checkbox and radio button

: checked selector is use to check status of checkbox and radio button.

<!doctype html>

<html lang=”en”>

<head>

<meta charset=”utf-8″>

<title>:checked Selector Example in jQuery</title>

<style>

div {

color: red;

}

</style>

<script src=”https://code.jquery.com/jquery-1.10.2.js”></script>

</head>

<body>

 

<form>

<p>

<input type=”checkbox” name=”checkedSelector” value=”1″ checked=”checked”>

 

<input type=”checkbox” name=”checkedSelector” value=”2″>

<input type=”checkbox” name=”checkedSelector” value=”3″ checked>

 

<input type=”checkbox” name=”checkedSelector” value=”4″ >

<input type=”checkbox” name=”checkedSelector” value=”5″>

</p>

</form>

<div></div>

 

<script>

var countChecked = function () {

var n = $(“input:checked”).length;

$(“div”).text(n + (n === 1 ? ” is” : ” are”) + ” checked!”);

};

countChecked();

 

$(“input[type=checkbox]”).on(“click”, countChecked);

</script>

 

</body>

</html>

Result :

Leave a Reply

Your email address will not be published. Required fields are marked *