Knowledge Walls
John Peter
Pune, Maharashtra, India
How to validate checkbox using Javascript and HTML with Example
12302 Views
HTML
<div>
Language:
    <input type="checkbox" name="chk_language" id="chk_hindi" value="hindi" />
    <label for="chk_hindi">Hindi</label>
    
    <input type="checkbox" name="chk_language" id="chk_english" value="english" checked />
    <label for="chk_english">English</label>

    <input type="checkbox" name="chk_language" id="chk_french"  value="french" />
    <label for="chk_french">French</label>

    <button onclick="javascript:validate();">Validate</button>
</div>

<div id="status">
</div>
JAVASCRIPT
function validate(){
    var elements = document.getElementsByName("chk_language");
    var statusText = "<br />";

    for (var index=0;index < elements.length;index++){
           statusText += elements[index].value+"-"+elements[index].checked+"<br />";
    }
    document.getElementById("status").innerHTML = statusText;
}
Output 
Demo 
Best Lessons of "Good Javascript Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details