I want to check what is the selected radio input.
here is my code.
<input name="u_type" type="radio" value="staff" id="u_type" checked="checked" /> Staff
<input name="u_type" type="radio" value="admin" id="u_type" /> Admin
<input id="add_user" name="add_user" type="button" onclick="addUser();" value="Add" class="submitButton admin_add" />
function addUser()
{
//how to check what is the selected radio input
}
thanks.
Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.
The value property sets or returns the value of the value attribute of the radio button. For radio buttons, the contents of the value property do not appear in the user interface. The value property only has meaning when submitting a form.
function addUser() {
//how to check what is the selected radio input
alert(getCheckedRadioValue('u_type'));
}
function getCheckedRadioValue(name) {
var elements = document.getElementsByName(name);
for (var i=0, len=elements.length; i<len; ++i)
if (elements[i].checked) return elements[i].value;
}
And element's IDs must be different.
To get the value of the checked radio button, without jQuery:
var radios = document.getElementsByName("u_type");
for(var i = 0; i < radios.length; i++) {
if(radios[i].checked) selectedValue = radios[i].value;
}
(assuming that selectedValue
is a variable declared elsewhere)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With