1. get the checked Radio
<div>
<label for="radioYes">What is your answer?</label>
<input type="radio" name="someRadioGroup" id="radioYes" value="yes" checked="checked"/> Yes
<input type="radio" name="someRadioGroup" id="radioNo" value="no"/> No
<input type="radio" name="someRadioGroup" id="radioMaybe" value="maybe"/> Maybe
<input type="radio" name="someRadioGroup" id="radioConfused" value="confused"/> I dunno
</div>
<div><button type="button" id="testButton"class="green90x24">Which?</button></div>
<div id="result"></div>
The old way to get the value:
var checkedValue ;
var elements = document.getElementsByTagName('input');
for(var n =0; n < elements.length; n++){
if(elements[n].type=='radio' && elements[n].name =='someRadioGroup' && elements[n].checked){
checkedValue = elements[n].value;
}
}
The new way to get the value:
var checkedValue = $('[name="someRadioGroup"].checked').val();
2.get the checkbox group values:
<div>
<label for="checkyesYes">What is your answer?</label>
<input type="checkbox" name="checkGroup" id="checkYes" value="yes" checked="checked"/> Yes
<input type="checkbox" name="checkGroup" id="checkNo" value="no"/> No
<input type="checkbox" name="checkGroup" id="checkMaybe" value="maybe"/> Maybe
</div>
the old way to get the values:
var checkedValue="";
var elements = document.myform.checkGroup;
for(var n =0; n < elements.length; n++){
if(elements[n].checked){
if(checkedValue==""){
checkedValue = checkedValue + elements[n].value;
}else{
checkedValue = checkedValue + ',' + elements[n].value;
}
}
}
the new way to get the values:
var checkedValue = $('[name="checkGroup"]:checked').map(
function(){return $(this).val();}
).toArray();
<div>
<label for="radioYes">What is your answer?</label>
<input type="radio" name="someRadioGroup" id="radioYes" value="yes" checked="checked"/> Yes
<input type="radio" name="someRadioGroup" id="radioNo" value="no"/> No
<input type="radio" name="someRadioGroup" id="radioMaybe" value="maybe"/> Maybe
<input type="radio" name="someRadioGroup" id="radioConfused" value="confused"/> I dunno
</div>
<div><button type="button" id="testButton"class="green90x24">Which?</button></div>
<div id="result"></div>
The old way to get the value:
var checkedValue ;
var elements = document.getElementsByTagName('input');
for(var n =0; n < elements.length; n++){
if(elements[n].type=='radio' && elements[n].name =='someRadioGroup' && elements[n].checked){
checkedValue = elements[n].value;
}
}
The new way to get the value:
var checkedValue = $('[name="someRadioGroup"].checked').val();
2.get the checkbox group values:
<div>
<label for="checkyesYes">What is your answer?</label>
<input type="checkbox" name="checkGroup" id="checkYes" value="yes" checked="checked"/> Yes
<input type="checkbox" name="checkGroup" id="checkNo" value="no"/> No
<input type="checkbox" name="checkGroup" id="checkMaybe" value="maybe"/> Maybe
</div>
the old way to get the values:
var checkedValue="";
var elements = document.myform.checkGroup;
for(var n =0; n < elements.length; n++){
if(elements[n].checked){
if(checkedValue==""){
checkedValue = checkedValue + elements[n].value;
}else{
checkedValue = checkedValue + ',' + elements[n].value;
}
}
}
the new way to get the values:
var checkedValue = $('[name="checkGroup"]:checked').map(
function(){return $(this).val();}
).toArray();
Comments
Post a Comment