Results 1 to 7 of 7

Thread: [RESOLVED] How to use Javascript to get the value of a radio button

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Resolved [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.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    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.

  3. #3
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    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:
    1. var radioButtons = document.getElementsByName("Vehicle");
    2. for (var i = 0; i < radioButtons.length; i++) {
    3.     if (radioButtons[i].checked) {
    4.         console.log("radioButton " + i + ": " + radioButtons[i].value);
    5.     }
    6. }
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  5. #5
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: How to use Javascript to get the value of a radio button

    Quote Originally Posted by akhileshbc View Post
    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'.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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
  •  



Click Here to Expand Forum to Full Width