Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
C#
I want to get radio button value using javascript.though i did getElementById but it is not showing me the value.please help


What I have tried:

I want to get radio button value using javascript.though i did getElementById but it is not showing me the value.please help
Posted
Updated 28-Apr-20 11:58am
Comments
Karthik_Mahalingam 23-Aug-16 6:00am    
show your code.

Try this :

var radios = document.getElementsByName('rbtn');

for (var i = 0, length = radios.length; i < length; i++) {
    if (radios[i].checked) {
        // do whatever you want with the checked radio
        alert(radios[i].value);

        // only one radio can be logically checked, don't check the rest
        break;
    }
}
 
Share this answer
 
See my sample here:

Markup
HTML
<input type="radio" value="1" name="choice">1</input>
<input type="radio" value="2" name="choice">2</input>
<input type="radio" value="3" name="choice">3</input>
<br/>
You selected: 
<label id="choiceLabel"></label>


JS
JavaScript
(function (){
    var radios = document.getElementsByName('choice');
    console.log(radios);
    for(var i = 0; i < radios.length; i++){
        radios[i].onclick = function(){
            document.getElementById('choiceLabel').innerText = this.value;
        }
    }
})();
 
Share this answer
 
I googled "get radio button value javascript" and quickly found this

javascript - How to get value of selected radio button? - Stack Overflow[^]

Please do basic research before asking a question, if that link doesn't help you there are many more if you google yourself.
 
Share this answer
 
Comments
F-ES Sitecore 27-Mar-20 15:37pm    
Thanks for your feedback on this 4 year old question. I shall cherish it :)
Emil Kozlev 4-Apr-20 6:51am    
Dude, you didn't deserve that harsh response. I hate to admit it, but I really reacted as an a*shole... I apologize.
var valueSelected=document.querySelector('input[name="my_input_radio_name"]:checked').value;
 
Share this answer
 
Comments
Emil Kozlev 27-Mar-20 14:35pm    
This thing works! Thanks, bro!
you could use Jquery to simplify your code
JavaScript
$('nameofradibutton).On('click', function() {

 // your algo
});
 
Share this answer
 
v2
You can radio value with js
JavaScript
var myRadio = document.getElementsByName("your-radio-name").value;
//Console the value
console.log(myRadio)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900