|
-
May 15th, 2012, 07:34 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] How to use Javascript to get the value of a radio button
I am trying to use Javascript to get the value of a radio button group.
In my code i have this radio group:
HTML Code:
<input type="radio" name="Vehicle" value="Car_Single" onchange="SelectFare()"/>
<input type="radio" name="Vehicle" value="Car_Return" onchange="SelectFare()"/>
<input type="radio" name="Vehicle" value="Estate_Single" />
<input type="radio" name="Vehicle" value="Estate_Return" />
<input type="radio" name="Vehicle" value="Exec_Single" />
<input type="radio" name="Vehicle" value="Exec_Return" />
<input type="radio" name="Vehicle" value="Carrier_Single" />
<input type="radio" name="Vehicle" value="Carrier_Return" />
<input type="radio" name="Vehicle" value="Bus_Single" />
<input type="radio" name="Vehicle" value="Bus_Return" />
I am using this Javascript code, but no message box shows:
Code:
function SelectFare() {
alert(document.getElementById("Vehicle").value);
}
I also tried this, but it only gives a message box saying undefined.
Code:
function SelectFare() {
alert(document.getElementsByName("Vehicle").value);
}
Thanks in advance, Chris1990.
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
If you fail, try and try again, its the only way to success.
-
May 15th, 2012, 08:30 PM
#2
Thread Starter
Hyperactive Member
Re: How to use Javascript to get the value of a radio button
I can now get the value of the radio button in to a message box using the code below. However when i use a variable, google chrome gives the error "Uncaught TypeError: Cannot read property 'checked' of undefined".
Code:
var Selected = "";
if (document.getElementsByName("Vehicle")[0].checked == true) {Selected = document.getElementsByName("Vehicle")[0].value;}
if (document.getElementsByName("Vehicle")[1].checked == true) {alert(document.getElementsByName("Vehicle")[1].value);}
if (document.getElementsByName("Vehicle")[2].checked == true) {alert(document.getElementsByName("Vehicle")[2].value);}
if (document.getElementsByName("Vehicle")[3].checked == true) {alert(document.getElementsByName("Vehicle")[3].value);}
if (document.getElementsByName("Vehicle")[4].checked == true) {alert(document.getElementsByName("Vehicle")[4].value);}
if (document.getElementsByName("Vehicle")[5].checked == true) {alert(document.getElementsByName("Vehicle")[5].value);}
if (document.getElementsByName("Vehicle")[6].checked == true) {alert(document.getElementsByName("Vehicle")[6].value);}
if (document.getElementsByName("Vehicle")[7].checked == true) {alert(document.getElementsByName("Vehicle")[7].value);}
if (document.getElementsByName("Vehicle")[8].checked == true) {alert(document.getElementsByName("Vehicle")[8].value);}
if (document.getElementsByName("Vehicle")[9].checked == true) {alert(document.getElementsByName("Vehicle")[9].value);}
if (document.getElementsByName("Vehicle")[10].checked == true) {alert(document.getElementsByName("Vehicle")[10].value);}
alert(Selected);
If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
If you fail, try and try again, its the only way to success.
-
May 15th, 2012, 09:10 PM
#3
Re: How to use Javascript to get the value of a radio button
Firstly, you cant select via document.getElementById() because you haven't specified any id attributes on any of your radio button input elements.
Your 'undefined' error is occurring because you only have 10 radio buttons defined, but you're trying to access 11 radio buttons (0-10 instead of 0-9). Don't forget about the zero-based indexing for the array of radio buttons.
It's probably easier and less error-prone to loop through all the radio buttons and see which are checked:
JavaScript Code:
var radioButtons = document.getElementsByName("Vehicle");
for (var i = 0; i < radioButtons.length; i++) {
if (radioButtons[i].checked) {
console.log("radioButton " + i + ": " + radioButtons[i].value);
}
}
-
May 15th, 2012, 11:04 PM
#4
Re: How to use Javascript to get the value of a radio button
If you are using jQuery, you could do it like this:
Code:
var selectedValue = $('input[name=Vehicle]:checked').val();
Live example: http://jsfiddle.net/eJWuT/
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
May 16th, 2012, 08:02 AM
#5
Re: How to use Javascript to get the value of a radio button
 Originally Posted by akhileshbc
If you are using jQuery, you could do it like this:
Code:
var selectedValue = $('input[name=Vehicle]:checked').val();
Live example: http://jsfiddle.net/eJWuT/

That will only work if one of the radio buttons is selected. If none are selected, then you will end up with 'undefined'.
-
May 16th, 2012, 08:12 AM
#6
Re: How to use Javascript to get the value of a radio button
An option button is used to select a single value.
So, the "undefined" issue can be cleared using an if statement to check it and alert the user with a message. Like this:
Code:
$('#checker').click(function(){ // Whenever a radiobox is clicked, the following code would be executed
var selectedValue = $('input[name=Vehicle]:checked').val(); // get the selected radio box's value
if(selectedValue === undefined) //check if user had selected an option
{
alert('Please select an option to continue !');
}
else{
alert(selectedValue); // display it
}
});
(I have updated the fiddle)
Another option is to put a default selection in one of the option boxes.
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
May 21st, 2012, 06:50 AM
#7
Re: How to use Javascript to get the value of a radio button
I assume you figured it out by now but you can do it by looping through the radio buttons to check which is selected.
Code:
<html>
<head>
<title>Radio Button Selection</title>
<script language="javascript">
function SelectFare() {
chosen = ""
len = document.frm.Vehicle.length
for (i = 0; i <len; i++) {
if (document.frm.Vehicle[i].checked) {
chosen = document.frm.Vehicle[i].value
}
}
alert(chosen)
}
</script>
</head>
<body>
<form name="frm">
<input type="radio" name="Vehicle" value="Car_Single" onclick="SelectFare()" />
<input type="radio" name="Vehicle" value="Car_Return" onclick="SelectFare()" />
<input type="radio" name="Vehicle" value="Estate_Single" onclick="SelectFare()" />
<input type="radio" name="Vehicle" value="Estate_Return" onclick="SelectFare()" />
<input type="radio" name="Vehicle" value="Exec_Single" onclick="SelectFare()" />
<input type="radio" name="Vehicle" value="Exec_Return" onclick="SelectFare()" />
<input type="radio" name="Vehicle" value="Carrier_Single" onclick="SelectFare()" />
<input type="radio" name="Vehicle" value="Carrier_Return" onclick="SelectFare()" />
<input type="radio" name="Vehicle" value="Bus_Single" onclick="SelectFare()" />
<input type="radio" name="Vehicle" value="Bus_Return" onclick="SelectFare()" />
</form>
</body>
</html>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|