Example code:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Get the value of checked checkbox?</h1>
<h2>Class name and name for one group should be same</h2>
<form action:"mailto:webwithjs@gmail.com" enctype="application/x-www-form-urlencoded" method = "post" autocomplete="on">
<br>checkbox<br>
<input class="messageCheckbox" type="checkbox" value="TV" id="tv" name="goods" checked>Television<br>
<input class="messageCheckbox" type="checkbox" value="Radio" id="radio" name="goods">Radio<br>
<br>Radio button<br>
<input class="messageCheckbox"type ="radio" name="best" value="ahutosh" checked> Ashutosh<br>
<input class="messageCheckbox" type ="radio" name="best" value="kumar " > Kumar<br>
<input class="messageCheckbox" type ="radio" name="best" value="singh" > Singh<br>
<br>CheckBox<br>
<input class="messageCheckbox" type="checkbox" value="3" name="num">Three<br>
<input class="messageCheckbox" type="checkbox" value="1" name="num">One<br>
<center>
<input type="submit" value="Ship It"><input type="reset" value="Clear Entries">
<!--<button type="button" onclick="f1()" >press</button>-->
<input type ="button" onclick="f1()" value="press">
</center>
<script language="JavaScript">
function f1(){
var checkedValue = " ";
var inputElements = document.getElementsByClassName('messageCheckbox');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValue +=" "+inputElements[i].value;
//break;
}
}
alert(checkedValue);
}
</script>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Get the value of checked checkbox?</h1>
<h2>Class name and name for one group should be same</h2>
<form action:"mailto:webwithjs@gmail.com" enctype="application/x-www-form-urlencoded" method = "post" autocomplete="on">
<br>checkbox<br>
<input class="messageCheckbox" type="checkbox" value="TV" id="tv" name="goods" checked>Television<br>
<input class="messageCheckbox" type="checkbox" value="Radio" id="radio" name="goods">Radio<br>
<br>Radio button<br>
<input class="messageCheckbox"type ="radio" name="best" value="ahutosh" checked> Ashutosh<br>
<input class="messageCheckbox" type ="radio" name="best" value="kumar " > Kumar<br>
<input class="messageCheckbox" type ="radio" name="best" value="singh" > Singh<br>
<br>CheckBox<br>
<input class="messageCheckbox" type="checkbox" value="3" name="num">Three<br>
<input class="messageCheckbox" type="checkbox" value="1" name="num">One<br>
<center>
<input type="submit" value="Ship It"><input type="reset" value="Clear Entries">
<!--<button type="button" onclick="f1()" >press</button>-->
<input type ="button" onclick="f1()" value="press">
</center>
<script language="JavaScript">
function f1(){
var checkedValue = " ";
var inputElements = document.getElementsByClassName('messageCheckbox');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValue +=" "+inputElements[i].value;
//break;
}
}
alert(checkedValue);
}
</script>
</body>
</html>
No comments:
Post a Comment