Sunday, July 1, 2018

JS:Get the value of checked checkbox/radio button?

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>


No comments:

Post a Comment

The depression in freezing point of water observed for the same amount of acetic acid, trichloroacetic acid and trifluoroacetic acid increases in the order given above. Explain briefly.

  Depression in freezing point is a colligative property that depends on the number of solute particles in a solution. Since the order of de...