So, you have a checkbox and you want to check if its selected or not at runtime using jQuery. If that's what you want then continue reading. A checkbox is an HTML element with type="checkbox" and checked property is used to find out whether a checkbox is selected or not. There are several ways to find if your checkbox has this property or not in jQuery e.g. by using :checked, a pseudo selector or by using is() jQuery function or by using prop() function which is available from jQuery 1.6 onward. In this article, we will see examples of these approaches to check if a check box is checked or not.
Checking if a checkbox is selected using prop()
// 1st example - by using prop function and checked property
if($('#isAgreed').prop('checked')){
$("#output").append("<h3>#checkbox is checked verified using prop() </h3>");
}
Checking if a checkbox is checked using is() function
// 2nd example - by using is function and :checked pseudo class
if($('#isAgreed').is(':checked')){
$("#output").append("<h3>#checkbox is checked verified using :checked() </h3>");
}
Finding if a checkbox is checked using attr() function
// 3rd example - using attr() function for jQuery version 1.5 and lover
if($('#isAgreed').attr('checked')){
$("#output").append("<h3>#checkbox is checked verified using attr() function </h3>");
}
Sample HTML and JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="//code.jquery.com/jquery-1.6.2.min.js"></script>
<title>How to check if a check-box is selected in jQuery</title>
</head>
<body>
<h2>3 ways to check status of check box using jQuery</h2>
<input type="checkbox" id="isAgreed" />
<div id="output"></div>
<script>
$('#isAgreed').click(function() {
// 1st example - by using prop function and checked property
if($('#isAgreed').prop('checked')){
$("#output").append("<h3>#checkbox is checked verified using prop() </h3>");
}
// 2nd example - by using is function and :checked pseudo class
if($('#isAgreed').is(':checked')){
$("#output").append("<h3>#checkbox is checked verified using :checked() </h3>");
}
// 3rd example - using attr() function for jQuery version 1.5 and lover
if($('#isAgreed').attr('checked')){
$("#output").append("<h3>#checkbox is checked verified using attr() function </h3>");
}
});
</script>
</body>
</html>
You can now click that checkbox and it will generate an event which will be captured by our jQuery code and it will print whether checkbox is selected or not as shown in next diagram:
Other jQuery tutorials and articles you may like
- How to get the current URL, Parameters, and Hash using jQuery? (solution)
- How to modify multiple elements in one go? (jquery tutorial)
- How to find all unchecked checkbox in jQuery? (tutorial)
- jQuery Hello World Example (program)
- How to enable/disable form elements in jQuery? (tutorial)
- How to use more than one jQuery UI DatePicker on the JSP page? (example)
- How to check/uncheck checkboxes in jQuery? (tutorial)
- How to create tab-based UI using jQuery? (example)
- How to redirect a page URL using jQuery and JavaScript? (solution)
- How to find all checked checkboxes in jQuery? (example)
- 20 jQuery Interview Questions for Java Web Developers (list)
- How to load jQuery on a web page? (solution)
- How to use multiple jQuery UI Date Picker in a page? (tutorial)
- How to solve jQuery - uncaught reference error $ is not defined error? (solution)
Do you know any other way to find out if a checkbox is selected or not in jQuery or just plain JavaScript?
jQuery rocks !!
ReplyDeleteI heard jQuery is dead?
ReplyDelete