Input Checkbox checked Property - Get the checked checkboxes in a form: - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Checkbox

Description

Input Checkbox checked Property - Get the checked checkboxes in a form:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>Please choose the following:</p>

<form action="#">
<input type="checkbox" name="coffee" value="cream">With cream<br>
<input type="checkbox" name="coffee" value="sugar">With sugar<br>
<br>
<input type="button" onclick="myFunction()" value="Send order">
<br><br>
<input type="text" id="order" size="50">
<input type="submit" value="Submit">
</form>/*from   w  w  w .ja  v  a  2s.c om*/

<script>
function myFunction() {
    var coffee = document.forms[0];
    var txt = "";
    var i;
    for (i = 0; i < coffee.length; i++) {
        if (coffee[i].checked) {
            txt = txt + coffee[i].value + " ";
        }
    }
    document.getElementById("order").value = "You ordered a coffee with: " + txt;
}
</script>

</body>
</html>

Related Tutorials