Knowledge Walls
John Peter
Pune, Maharashtra, India
How to validate radio box using Javascript and HTML with Example
3404 Views
HTML
<div>
<h2>Which is your favorite language:</h2>
    <input type="radio" name="chk_language" id="chk_hindi" value="hindi" />
    <label for="chk_hindi">Hindi</label>
    
    <input type="radio" name="chk_language" id="chk_english" value="english" checked />
    <label for="chk_english">English</label>

    <input type="radio" 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