Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check whether a checkbox is checked with JavaScript?
To check whether a checkbox is checked with JavaScript, the code is as follows −
Example
<!DOCTYPE html>
<html>
<head>
<h1>Displaying textBox when a checkbox is checked</h1>
Checkbox: <input type="checkbox" class="check" onclick="checkFunction()" />
<h2 class="textBox" style="display:none">Checkbox is checked!!!</h2>
<script>
document.querySelector(".check").addEventListener("click", checkFunction);
function checkFunction() {
var checkBox = document.querySelector(".check");
var textBox = document.querySelector(".textBox");
if (checkBox.checked == true) {
textBox.style.display = "block";
} else {
textBox.style.display = "none";
}
}
</script>
</body>
</html>
Output
This will produce the following output −

On clicking the checkbox −

Advertisements