How to get the selected radio button value using js
I am using this code to get the value of currently selected radio button, but it doesn't work.
var mailcopy = document.getElementById('mailCopy').value;
How to get the currently selected radio button value using Javascript?
javascript radio-button
add a comment |
I am using this code to get the value of currently selected radio button, but it doesn't work.
var mailcopy = document.getElementById('mailCopy').value;
How to get the currently selected radio button value using Javascript?
javascript radio-button
If this method does work already, why're you looking for another?
– Nikita Rybak
Oct 6 '10 at 4:46
2
I think there was a typo that left out a "not"
– Quentin
Oct 6 '10 at 6:28
the question should change to "it does NOT return the selected..."!
– GMsoF
Apr 22 '13 at 6:47
1
Is this still an problem, as you have not accepted an answer!
– pattyd
Jul 22 '13 at 20:36
add a comment |
I am using this code to get the value of currently selected radio button, but it doesn't work.
var mailcopy = document.getElementById('mailCopy').value;
How to get the currently selected radio button value using Javascript?
javascript radio-button
I am using this code to get the value of currently selected radio button, but it doesn't work.
var mailcopy = document.getElementById('mailCopy').value;
How to get the currently selected radio button value using Javascript?
javascript radio-button
javascript radio-button
edited Sep 26 '16 at 16:22
Damjan Pavlica
8,78952752
8,78952752
asked Oct 6 '10 at 4:40
MeenaMeena
50241634
50241634
If this method does work already, why're you looking for another?
– Nikita Rybak
Oct 6 '10 at 4:46
2
I think there was a typo that left out a "not"
– Quentin
Oct 6 '10 at 6:28
the question should change to "it does NOT return the selected..."!
– GMsoF
Apr 22 '13 at 6:47
1
Is this still an problem, as you have not accepted an answer!
– pattyd
Jul 22 '13 at 20:36
add a comment |
If this method does work already, why're you looking for another?
– Nikita Rybak
Oct 6 '10 at 4:46
2
I think there was a typo that left out a "not"
– Quentin
Oct 6 '10 at 6:28
the question should change to "it does NOT return the selected..."!
– GMsoF
Apr 22 '13 at 6:47
1
Is this still an problem, as you have not accepted an answer!
– pattyd
Jul 22 '13 at 20:36
If this method does work already, why're you looking for another?
– Nikita Rybak
Oct 6 '10 at 4:46
If this method does work already, why're you looking for another?
– Nikita Rybak
Oct 6 '10 at 4:46
2
2
I think there was a typo that left out a "not"
– Quentin
Oct 6 '10 at 6:28
I think there was a typo that left out a "not"
– Quentin
Oct 6 '10 at 6:28
the question should change to "it does NOT return the selected..."!
– GMsoF
Apr 22 '13 at 6:47
the question should change to "it does NOT return the selected..."!
– GMsoF
Apr 22 '13 at 6:47
1
1
Is this still an problem, as you have not accepted an answer!
– pattyd
Jul 22 '13 at 20:36
Is this still an problem, as you have not accepted an answer!
– pattyd
Jul 22 '13 at 20:36
add a comment |
17 Answers
17
active
oldest
votes
HTML
<p>Gender</p>
<input type="radio" id="gender0" name="gender" value="Male">Male<br>
<input type="radio" id="gender1" name="gender" value="Female">Female<br>
JS
var gender = document.querySelector('input[name = "gender"]:checked').value;
document.writeln("You entered " + gender + " for your gender<br>");
Lovely. And no need for a parent element.
– AlikElzin-kilaka
Jan 7 '15 at 15:33
6
You can't have two elements with the same id.
– mbomb007
Apr 23 '15 at 15:32
@mbomb007 i don't see two elements having same id here
– Nabeel Khan
May 14 '16 at 7:39
3
@NabeelKhan The answer was edited after I said that. View the edit history.
– mbomb007
May 15 '16 at 0:48
add a comment |
If you are using jQuery, following code will work for you.
$('input[name=radioName]:checked').val();
add a comment |
Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.
function getCheckedRadio(radio_group) {
for (var i = 0; i < radio_group.length; i++) {
var button = radio_group[i];
if (button.checked) {
return button;
}
}
return undefined;
}
var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);
if (checkedButton) {
alert("The value is " + checkedButton.value);
}
Good for compatibility, this method works for older browsers too, pre HTML 5.
– j.c
Nov 25 '16 at 5:03
add a comment |
check this
<input class="gender" type="radio" name="sex" value="male">Male
<br>
<input class="gender" type="radio" name="sex" value="female">Female
<script type="text/javascript">
$(document).ready(function () {
$(".gender").change(function () {
var val = $('.gender:checked').val();
alert(val);
});
});
</script>
Example
my requirement is quite different, the alert box will come but at the click on the button
– Akash
Oct 30 '15 at 11:31
add a comment |
Maybe I'm missing something here, but wouldn't the good old standard JS work? I mean:
var selectedOption = document.getElementById('your-form-name')['radio-group-name'].value;
... which is only valid of course if have provided "value" for your radio input elements.
<input type="radio" name="radio-group-name" value="red" checked>
<input type="radio" name="radio-group-name" value="blue">
The value should be either 'red' or 'blue' in the above example.
add a comment |
function getCheckedValue(radioObj, name) {
for (j = 0; j < radioObj.rows.length; ++j) {
for (k = 0; k < radioObj.cells.length; ++k) {
var radioChoice = document.getElementById(name + "_" + k);
if (radioChoice.checked) {
return radioChoice.value;
}
}
}
return "";
}
add a comment |
A simpler way of doing this is to use a global js variable that simply holds the id of the clicked radio button. Then you don't have to waste code spinning thru your radio lists looking for the selected value.
I have done this in cases where I have a dynamically generated list of 100 or more radio buttons. spinning thru them is (almost imperceptible) slow, but this is an easier solution.
you can also do this with the id, but you usually just want the value.
<script>
var gRadioValue = ''; //global declared outside of function
function myRadioFunc(){
var radioVal = gRadioValue;
// or maybe: var whichRadio = document.getElementById(gWhichCheckedId);
//do somethign with radioVal
}
<script>
<input type="radio" name="rdo" id="rdo1" value="1" onClick="gRadioValue =this.value;"> One
<input type="radio" name="rdo" id="rdo2" value="2" onClick="gRadioValue =this.value;"> Two
...
<input type="radio" name="rdo" id="rdo99" value="99" onClick="gRadioValue =this.value;"> 99
add a comment |
you can use this
$('input[name="field_value"]:checked').val();
or, for older version of jquery
$('input[@name="field_value"]:checked').val();
add a comment |
Since you want to get it using plain javascript, you can use the following code
var val = '';
if(document.getElementById('radio1').checked) {
val = document.getElementById('radio1').value
}else if(document.getElementById('radio2').checked) {
val = document.getElementById('radio2').value
}
add a comment |
Possibly not the most efficient way... but I have used an ID on each radio button (this is just because I'm not passing it as an actual form, it is just the raw fields).
I then call a function with a button, which checks each radio button to see if it is checked. It does this using the .checked
function. If this is set to true I can change the value of another variable.
function createOutput() {
var order = "in";
if (document.getElementById('radio1').checked == true) {
order = "pre";
} else if (document.getElementById('radio2').checked == true) {
order = "in";
} else if (document.getElementById('radio3').checked == true) {
order = "post";
}
document.getElementById('outputBox').innerHTML = order;
}
<input id="radio1" type="radio" name="order" value="preorder" />Preorder
<input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
<input id="radio3" type="radio" name="order" value="postorder" />Postorder
<button onclick="createOutput();">Generate Output</button>
<textarea id="outputBox" rows="10" cols="50"></textarea>
Hope this is useful, in someway,
Beanz
add a comment |
You could do something very similar to Beanz's answer but instead of using IDs, use classes to reduce redundancy.
function getSelectedValue() {
var radioBtns = document.getElementsByClassName("radioBtn");
for(var i = 0; i < radioBtns.length; i++){
if(radioBtns[i].checked){
document.getElementById("output").textContent = radioBtns[i].value;
}
}
}
<input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
<input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
<input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
<button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
<textarea id="output"></textarea>
add a comment |
all of you can test this example and easy to understand.
Name: <input type="text" id="text" class ="input">
<input type="text" id="text1" class ="input">
Gender: <input type="radio" id="m" class="Rm" name="Rmale" value="Male">
<input type="radio" id="f" class="Rm" name="Rfemale" value="Female">
Course: <input type="checkbox" id="math" class="cm" name="Cmath" value="Math">
<input type="checkbox" id="physic" class="cm" name="Cphysic" value="Physic">
<input type="checkbox" id="eng" class="cm" name="Ceng" value="English">
<button type="button" id="b1">show</button>
// javascript
<script>
function getData(input){
return document.getElementById(input).value;
}
function dataByClassName(st){
var value=document.getElementsByClassName(st)
for(var i=0;i < value.length;i++){
if(value[i].checked){
return value[i].value;
}
}
}
document.getElementById("b1").onclick = function ()
{
var st={
name : getData("text")+getData("text1"),
gender : dataByClassName("Rm"),
course : dataByClassName("cm")
};
alert(st.name+" "+st.gender+" "+st.course);
};
</script>
add a comment |
Try this, I hope this one will work
function loadRadioButton(objectName, selectedValue) {
var radioButtons = document.getElementsByName(objectName);
if (radioButtons != null) {
for (var radioCount = 0; radioCount < radioButtons.length; radioCount++) {
if (radioButtons[radioCount].value == selectedValue) {
radioButtons[radioCount].checked = true;
}
}
}
}
add a comment |
If you can use jQuery "Chamika Sandamal" answer is the correct way to go. In the case you can't use jQuery you can do something like this:
function selectedRadio() {
var radio = document.getElementsByName('mailCopy');
alert(radio[0].value);
}
Notes:
- In general for the inputs you want to have unique IDs (not a requirement but a good practice)
- All the radio inputs that are from the same group MUST have the same name attribute, for example
- You have to set the value attribute for each input
Here is an example of input radios:
<input type="radio" name="mailCopy" value="1" />1<br />
<input type="radio" name="mailCopy" value="2" />2<br />
add a comment |
var mailcopy = document.getElementById('mailCopy').checked;
if(mailcopy==true)
{
alert("Radio Button Checked");
}
else
{
alert("Radio Button un-Checked");
}
3
The question is looking to get the currently selected button, not find out if a specific button is checked.
– Quentin
Oct 6 '10 at 6:23
add a comment |
Hy, you have to do it this way.
function checkRadio () {
if(document.getElementById('user1').checked) {
return $('#user1').val();
}else if(document.getElementById('user2').checked) {
return $('#user2').val();
}
}
add a comment |
Use the element.checked
property.
thank u for your reply . can u explain in clear
– Meena
Oct 6 '10 at 5:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3869535%2fhow-to-get-the-selected-radio-button-value-using-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
17 Answers
17
active
oldest
votes
17 Answers
17
active
oldest
votes
active
oldest
votes
active
oldest
votes
HTML
<p>Gender</p>
<input type="radio" id="gender0" name="gender" value="Male">Male<br>
<input type="radio" id="gender1" name="gender" value="Female">Female<br>
JS
var gender = document.querySelector('input[name = "gender"]:checked').value;
document.writeln("You entered " + gender + " for your gender<br>");
Lovely. And no need for a parent element.
– AlikElzin-kilaka
Jan 7 '15 at 15:33
6
You can't have two elements with the same id.
– mbomb007
Apr 23 '15 at 15:32
@mbomb007 i don't see two elements having same id here
– Nabeel Khan
May 14 '16 at 7:39
3
@NabeelKhan The answer was edited after I said that. View the edit history.
– mbomb007
May 15 '16 at 0:48
add a comment |
HTML
<p>Gender</p>
<input type="radio" id="gender0" name="gender" value="Male">Male<br>
<input type="radio" id="gender1" name="gender" value="Female">Female<br>
JS
var gender = document.querySelector('input[name = "gender"]:checked').value;
document.writeln("You entered " + gender + " for your gender<br>");
Lovely. And no need for a parent element.
– AlikElzin-kilaka
Jan 7 '15 at 15:33
6
You can't have two elements with the same id.
– mbomb007
Apr 23 '15 at 15:32
@mbomb007 i don't see two elements having same id here
– Nabeel Khan
May 14 '16 at 7:39
3
@NabeelKhan The answer was edited after I said that. View the edit history.
– mbomb007
May 15 '16 at 0:48
add a comment |
HTML
<p>Gender</p>
<input type="radio" id="gender0" name="gender" value="Male">Male<br>
<input type="radio" id="gender1" name="gender" value="Female">Female<br>
JS
var gender = document.querySelector('input[name = "gender"]:checked').value;
document.writeln("You entered " + gender + " for your gender<br>");
HTML
<p>Gender</p>
<input type="radio" id="gender0" name="gender" value="Male">Male<br>
<input type="radio" id="gender1" name="gender" value="Female">Female<br>
JS
var gender = document.querySelector('input[name = "gender"]:checked').value;
document.writeln("You entered " + gender + " for your gender<br>");
edited Nov 10 '17 at 8:33
orange01
450714
450714
answered Jul 22 '13 at 20:28
Mihai AlinMihai Alin
81964
81964
Lovely. And no need for a parent element.
– AlikElzin-kilaka
Jan 7 '15 at 15:33
6
You can't have two elements with the same id.
– mbomb007
Apr 23 '15 at 15:32
@mbomb007 i don't see two elements having same id here
– Nabeel Khan
May 14 '16 at 7:39
3
@NabeelKhan The answer was edited after I said that. View the edit history.
– mbomb007
May 15 '16 at 0:48
add a comment |
Lovely. And no need for a parent element.
– AlikElzin-kilaka
Jan 7 '15 at 15:33
6
You can't have two elements with the same id.
– mbomb007
Apr 23 '15 at 15:32
@mbomb007 i don't see two elements having same id here
– Nabeel Khan
May 14 '16 at 7:39
3
@NabeelKhan The answer was edited after I said that. View the edit history.
– mbomb007
May 15 '16 at 0:48
Lovely. And no need for a parent element.
– AlikElzin-kilaka
Jan 7 '15 at 15:33
Lovely. And no need for a parent element.
– AlikElzin-kilaka
Jan 7 '15 at 15:33
6
6
You can't have two elements with the same id.
– mbomb007
Apr 23 '15 at 15:32
You can't have two elements with the same id.
– mbomb007
Apr 23 '15 at 15:32
@mbomb007 i don't see two elements having same id here
– Nabeel Khan
May 14 '16 at 7:39
@mbomb007 i don't see two elements having same id here
– Nabeel Khan
May 14 '16 at 7:39
3
3
@NabeelKhan The answer was edited after I said that. View the edit history.
– mbomb007
May 15 '16 at 0:48
@NabeelKhan The answer was edited after I said that. View the edit history.
– mbomb007
May 15 '16 at 0:48
add a comment |
If you are using jQuery, following code will work for you.
$('input[name=radioName]:checked').val();
add a comment |
If you are using jQuery, following code will work for you.
$('input[name=radioName]:checked').val();
add a comment |
If you are using jQuery, following code will work for you.
$('input[name=radioName]:checked').val();
If you are using jQuery, following code will work for you.
$('input[name=radioName]:checked').val();
answered Sep 24 '12 at 10:49
Chamika SandamalChamika Sandamal
19.4k35176
19.4k35176
add a comment |
add a comment |
Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.
function getCheckedRadio(radio_group) {
for (var i = 0; i < radio_group.length; i++) {
var button = radio_group[i];
if (button.checked) {
return button;
}
}
return undefined;
}
var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);
if (checkedButton) {
alert("The value is " + checkedButton.value);
}
Good for compatibility, this method works for older browsers too, pre HTML 5.
– j.c
Nov 25 '16 at 5:03
add a comment |
Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.
function getCheckedRadio(radio_group) {
for (var i = 0; i < radio_group.length; i++) {
var button = radio_group[i];
if (button.checked) {
return button;
}
}
return undefined;
}
var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);
if (checkedButton) {
alert("The value is " + checkedButton.value);
}
Good for compatibility, this method works for older browsers too, pre HTML 5.
– j.c
Nov 25 '16 at 5:03
add a comment |
Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.
function getCheckedRadio(radio_group) {
for (var i = 0; i < radio_group.length; i++) {
var button = radio_group[i];
if (button.checked) {
return button;
}
}
return undefined;
}
var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);
if (checkedButton) {
alert("The value is " + checkedButton.value);
}
Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.
function getCheckedRadio(radio_group) {
for (var i = 0; i < radio_group.length; i++) {
var button = radio_group[i];
if (button.checked) {
return button;
}
}
return undefined;
}
var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);
if (checkedButton) {
alert("The value is " + checkedButton.value);
}
answered Oct 6 '10 at 6:22
QuentinQuentin
646k718761041
646k718761041
Good for compatibility, this method works for older browsers too, pre HTML 5.
– j.c
Nov 25 '16 at 5:03
add a comment |
Good for compatibility, this method works for older browsers too, pre HTML 5.
– j.c
Nov 25 '16 at 5:03
Good for compatibility, this method works for older browsers too, pre HTML 5.
– j.c
Nov 25 '16 at 5:03
Good for compatibility, this method works for older browsers too, pre HTML 5.
– j.c
Nov 25 '16 at 5:03
add a comment |
check this
<input class="gender" type="radio" name="sex" value="male">Male
<br>
<input class="gender" type="radio" name="sex" value="female">Female
<script type="text/javascript">
$(document).ready(function () {
$(".gender").change(function () {
var val = $('.gender:checked').val();
alert(val);
});
});
</script>
Example
my requirement is quite different, the alert box will come but at the click on the button
– Akash
Oct 30 '15 at 11:31
add a comment |
check this
<input class="gender" type="radio" name="sex" value="male">Male
<br>
<input class="gender" type="radio" name="sex" value="female">Female
<script type="text/javascript">
$(document).ready(function () {
$(".gender").change(function () {
var val = $('.gender:checked').val();
alert(val);
});
});
</script>
Example
my requirement is quite different, the alert box will come but at the click on the button
– Akash
Oct 30 '15 at 11:31
add a comment |
check this
<input class="gender" type="radio" name="sex" value="male">Male
<br>
<input class="gender" type="radio" name="sex" value="female">Female
<script type="text/javascript">
$(document).ready(function () {
$(".gender").change(function () {
var val = $('.gender:checked').val();
alert(val);
});
});
</script>
Example
check this
<input class="gender" type="radio" name="sex" value="male">Male
<br>
<input class="gender" type="radio" name="sex" value="female">Female
<script type="text/javascript">
$(document).ready(function () {
$(".gender").change(function () {
var val = $('.gender:checked').val();
alert(val);
});
});
</script>
Example
answered Feb 25 '13 at 11:35
SanjaySanjay
6321321
6321321
my requirement is quite different, the alert box will come but at the click on the button
– Akash
Oct 30 '15 at 11:31
add a comment |
my requirement is quite different, the alert box will come but at the click on the button
– Akash
Oct 30 '15 at 11:31
my requirement is quite different, the alert box will come but at the click on the button
– Akash
Oct 30 '15 at 11:31
my requirement is quite different, the alert box will come but at the click on the button
– Akash
Oct 30 '15 at 11:31
add a comment |
Maybe I'm missing something here, but wouldn't the good old standard JS work? I mean:
var selectedOption = document.getElementById('your-form-name')['radio-group-name'].value;
... which is only valid of course if have provided "value" for your radio input elements.
<input type="radio" name="radio-group-name" value="red" checked>
<input type="radio" name="radio-group-name" value="blue">
The value should be either 'red' or 'blue' in the above example.
add a comment |
Maybe I'm missing something here, but wouldn't the good old standard JS work? I mean:
var selectedOption = document.getElementById('your-form-name')['radio-group-name'].value;
... which is only valid of course if have provided "value" for your radio input elements.
<input type="radio" name="radio-group-name" value="red" checked>
<input type="radio" name="radio-group-name" value="blue">
The value should be either 'red' or 'blue' in the above example.
add a comment |
Maybe I'm missing something here, but wouldn't the good old standard JS work? I mean:
var selectedOption = document.getElementById('your-form-name')['radio-group-name'].value;
... which is only valid of course if have provided "value" for your radio input elements.
<input type="radio" name="radio-group-name" value="red" checked>
<input type="radio" name="radio-group-name" value="blue">
The value should be either 'red' or 'blue' in the above example.
Maybe I'm missing something here, but wouldn't the good old standard JS work? I mean:
var selectedOption = document.getElementById('your-form-name')['radio-group-name'].value;
... which is only valid of course if have provided "value" for your radio input elements.
<input type="radio" name="radio-group-name" value="red" checked>
<input type="radio" name="radio-group-name" value="blue">
The value should be either 'red' or 'blue' in the above example.
answered Jan 17 '16 at 11:48
Stan1eyStan1ey
311
311
add a comment |
add a comment |
function getCheckedValue(radioObj, name) {
for (j = 0; j < radioObj.rows.length; ++j) {
for (k = 0; k < radioObj.cells.length; ++k) {
var radioChoice = document.getElementById(name + "_" + k);
if (radioChoice.checked) {
return radioChoice.value;
}
}
}
return "";
}
add a comment |
function getCheckedValue(radioObj, name) {
for (j = 0; j < radioObj.rows.length; ++j) {
for (k = 0; k < radioObj.cells.length; ++k) {
var radioChoice = document.getElementById(name + "_" + k);
if (radioChoice.checked) {
return radioChoice.value;
}
}
}
return "";
}
add a comment |
function getCheckedValue(radioObj, name) {
for (j = 0; j < radioObj.rows.length; ++j) {
for (k = 0; k < radioObj.cells.length; ++k) {
var radioChoice = document.getElementById(name + "_" + k);
if (radioChoice.checked) {
return radioChoice.value;
}
}
}
return "";
}
function getCheckedValue(radioObj, name) {
for (j = 0; j < radioObj.rows.length; ++j) {
for (k = 0; k < radioObj.cells.length; ++k) {
var radioChoice = document.getElementById(name + "_" + k);
if (radioChoice.checked) {
return radioChoice.value;
}
}
}
return "";
}
edited Sep 24 '12 at 11:10
Littm
4,71732333
4,71732333
answered Sep 24 '12 at 10:45
Abhaysingh N. RajpurohitAbhaysingh N. Rajpurohit
211
211
add a comment |
add a comment |
A simpler way of doing this is to use a global js variable that simply holds the id of the clicked radio button. Then you don't have to waste code spinning thru your radio lists looking for the selected value.
I have done this in cases where I have a dynamically generated list of 100 or more radio buttons. spinning thru them is (almost imperceptible) slow, but this is an easier solution.
you can also do this with the id, but you usually just want the value.
<script>
var gRadioValue = ''; //global declared outside of function
function myRadioFunc(){
var radioVal = gRadioValue;
// or maybe: var whichRadio = document.getElementById(gWhichCheckedId);
//do somethign with radioVal
}
<script>
<input type="radio" name="rdo" id="rdo1" value="1" onClick="gRadioValue =this.value;"> One
<input type="radio" name="rdo" id="rdo2" value="2" onClick="gRadioValue =this.value;"> Two
...
<input type="radio" name="rdo" id="rdo99" value="99" onClick="gRadioValue =this.value;"> 99
add a comment |
A simpler way of doing this is to use a global js variable that simply holds the id of the clicked radio button. Then you don't have to waste code spinning thru your radio lists looking for the selected value.
I have done this in cases where I have a dynamically generated list of 100 or more radio buttons. spinning thru them is (almost imperceptible) slow, but this is an easier solution.
you can also do this with the id, but you usually just want the value.
<script>
var gRadioValue = ''; //global declared outside of function
function myRadioFunc(){
var radioVal = gRadioValue;
// or maybe: var whichRadio = document.getElementById(gWhichCheckedId);
//do somethign with radioVal
}
<script>
<input type="radio" name="rdo" id="rdo1" value="1" onClick="gRadioValue =this.value;"> One
<input type="radio" name="rdo" id="rdo2" value="2" onClick="gRadioValue =this.value;"> Two
...
<input type="radio" name="rdo" id="rdo99" value="99" onClick="gRadioValue =this.value;"> 99
add a comment |
A simpler way of doing this is to use a global js variable that simply holds the id of the clicked radio button. Then you don't have to waste code spinning thru your radio lists looking for the selected value.
I have done this in cases where I have a dynamically generated list of 100 or more radio buttons. spinning thru them is (almost imperceptible) slow, but this is an easier solution.
you can also do this with the id, but you usually just want the value.
<script>
var gRadioValue = ''; //global declared outside of function
function myRadioFunc(){
var radioVal = gRadioValue;
// or maybe: var whichRadio = document.getElementById(gWhichCheckedId);
//do somethign with radioVal
}
<script>
<input type="radio" name="rdo" id="rdo1" value="1" onClick="gRadioValue =this.value;"> One
<input type="radio" name="rdo" id="rdo2" value="2" onClick="gRadioValue =this.value;"> Two
...
<input type="radio" name="rdo" id="rdo99" value="99" onClick="gRadioValue =this.value;"> 99
A simpler way of doing this is to use a global js variable that simply holds the id of the clicked radio button. Then you don't have to waste code spinning thru your radio lists looking for the selected value.
I have done this in cases where I have a dynamically generated list of 100 or more radio buttons. spinning thru them is (almost imperceptible) slow, but this is an easier solution.
you can also do this with the id, but you usually just want the value.
<script>
var gRadioValue = ''; //global declared outside of function
function myRadioFunc(){
var radioVal = gRadioValue;
// or maybe: var whichRadio = document.getElementById(gWhichCheckedId);
//do somethign with radioVal
}
<script>
<input type="radio" name="rdo" id="rdo1" value="1" onClick="gRadioValue =this.value;"> One
<input type="radio" name="rdo" id="rdo2" value="2" onClick="gRadioValue =this.value;"> Two
...
<input type="radio" name="rdo" id="rdo99" value="99" onClick="gRadioValue =this.value;"> 99
edited Aug 30 '13 at 18:14
answered Aug 30 '13 at 17:58
AlanAlan
24628
24628
add a comment |
add a comment |
you can use this
$('input[name="field_value"]:checked').val();
or, for older version of jquery
$('input[@name="field_value"]:checked').val();
add a comment |
you can use this
$('input[name="field_value"]:checked').val();
or, for older version of jquery
$('input[@name="field_value"]:checked').val();
add a comment |
you can use this
$('input[name="field_value"]:checked').val();
or, for older version of jquery
$('input[@name="field_value"]:checked').val();
you can use this
$('input[name="field_value"]:checked').val();
or, for older version of jquery
$('input[@name="field_value"]:checked').val();
edited Jun 20 '13 at 6:24
Andrea
7,661144652
7,661144652
answered Jun 20 '13 at 6:07
Mughal SahabMughal Sahab
312
312
add a comment |
add a comment |
Since you want to get it using plain javascript, you can use the following code
var val = '';
if(document.getElementById('radio1').checked) {
val = document.getElementById('radio1').value
}else if(document.getElementById('radio2').checked) {
val = document.getElementById('radio2').value
}
add a comment |
Since you want to get it using plain javascript, you can use the following code
var val = '';
if(document.getElementById('radio1').checked) {
val = document.getElementById('radio1').value
}else if(document.getElementById('radio2').checked) {
val = document.getElementById('radio2').value
}
add a comment |
Since you want to get it using plain javascript, you can use the following code
var val = '';
if(document.getElementById('radio1').checked) {
val = document.getElementById('radio1').value
}else if(document.getElementById('radio2').checked) {
val = document.getElementById('radio2').value
}
Since you want to get it using plain javascript, you can use the following code
var val = '';
if(document.getElementById('radio1').checked) {
val = document.getElementById('radio1').value
}else if(document.getElementById('radio2').checked) {
val = document.getElementById('radio2').value
}
answered Jan 21 '14 at 9:59
NauphalNauphal
5,53732143
5,53732143
add a comment |
add a comment |
Possibly not the most efficient way... but I have used an ID on each radio button (this is just because I'm not passing it as an actual form, it is just the raw fields).
I then call a function with a button, which checks each radio button to see if it is checked. It does this using the .checked
function. If this is set to true I can change the value of another variable.
function createOutput() {
var order = "in";
if (document.getElementById('radio1').checked == true) {
order = "pre";
} else if (document.getElementById('radio2').checked == true) {
order = "in";
} else if (document.getElementById('radio3').checked == true) {
order = "post";
}
document.getElementById('outputBox').innerHTML = order;
}
<input id="radio1" type="radio" name="order" value="preorder" />Preorder
<input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
<input id="radio3" type="radio" name="order" value="postorder" />Postorder
<button onclick="createOutput();">Generate Output</button>
<textarea id="outputBox" rows="10" cols="50"></textarea>
Hope this is useful, in someway,
Beanz
add a comment |
Possibly not the most efficient way... but I have used an ID on each radio button (this is just because I'm not passing it as an actual form, it is just the raw fields).
I then call a function with a button, which checks each radio button to see if it is checked. It does this using the .checked
function. If this is set to true I can change the value of another variable.
function createOutput() {
var order = "in";
if (document.getElementById('radio1').checked == true) {
order = "pre";
} else if (document.getElementById('radio2').checked == true) {
order = "in";
} else if (document.getElementById('radio3').checked == true) {
order = "post";
}
document.getElementById('outputBox').innerHTML = order;
}
<input id="radio1" type="radio" name="order" value="preorder" />Preorder
<input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
<input id="radio3" type="radio" name="order" value="postorder" />Postorder
<button onclick="createOutput();">Generate Output</button>
<textarea id="outputBox" rows="10" cols="50"></textarea>
Hope this is useful, in someway,
Beanz
add a comment |
Possibly not the most efficient way... but I have used an ID on each radio button (this is just because I'm not passing it as an actual form, it is just the raw fields).
I then call a function with a button, which checks each radio button to see if it is checked. It does this using the .checked
function. If this is set to true I can change the value of another variable.
function createOutput() {
var order = "in";
if (document.getElementById('radio1').checked == true) {
order = "pre";
} else if (document.getElementById('radio2').checked == true) {
order = "in";
} else if (document.getElementById('radio3').checked == true) {
order = "post";
}
document.getElementById('outputBox').innerHTML = order;
}
<input id="radio1" type="radio" name="order" value="preorder" />Preorder
<input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
<input id="radio3" type="radio" name="order" value="postorder" />Postorder
<button onclick="createOutput();">Generate Output</button>
<textarea id="outputBox" rows="10" cols="50"></textarea>
Hope this is useful, in someway,
Beanz
Possibly not the most efficient way... but I have used an ID on each radio button (this is just because I'm not passing it as an actual form, it is just the raw fields).
I then call a function with a button, which checks each radio button to see if it is checked. It does this using the .checked
function. If this is set to true I can change the value of another variable.
function createOutput() {
var order = "in";
if (document.getElementById('radio1').checked == true) {
order = "pre";
} else if (document.getElementById('radio2').checked == true) {
order = "in";
} else if (document.getElementById('radio3').checked == true) {
order = "post";
}
document.getElementById('outputBox').innerHTML = order;
}
<input id="radio1" type="radio" name="order" value="preorder" />Preorder
<input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
<input id="radio3" type="radio" name="order" value="postorder" />Postorder
<button onclick="createOutput();">Generate Output</button>
<textarea id="outputBox" rows="10" cols="50"></textarea>
Hope this is useful, in someway,
Beanz
function createOutput() {
var order = "in";
if (document.getElementById('radio1').checked == true) {
order = "pre";
} else if (document.getElementById('radio2').checked == true) {
order = "in";
} else if (document.getElementById('radio3').checked == true) {
order = "post";
}
document.getElementById('outputBox').innerHTML = order;
}
<input id="radio1" type="radio" name="order" value="preorder" />Preorder
<input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
<input id="radio3" type="radio" name="order" value="postorder" />Postorder
<button onclick="createOutput();">Generate Output</button>
<textarea id="outputBox" rows="10" cols="50"></textarea>
function createOutput() {
var order = "in";
if (document.getElementById('radio1').checked == true) {
order = "pre";
} else if (document.getElementById('radio2').checked == true) {
order = "in";
} else if (document.getElementById('radio3').checked == true) {
order = "post";
}
document.getElementById('outputBox').innerHTML = order;
}
<input id="radio1" type="radio" name="order" value="preorder" />Preorder
<input id="radio2" type="radio" name="order" value="inorder" checked="true" />Inorder
<input id="radio3" type="radio" name="order" value="postorder" />Postorder
<button onclick="createOutput();">Generate Output</button>
<textarea id="outputBox" rows="10" cols="50"></textarea>
answered Mar 4 '16 at 22:49
Breandán FawcettBreandán Fawcett
64
64
add a comment |
add a comment |
You could do something very similar to Beanz's answer but instead of using IDs, use classes to reduce redundancy.
function getSelectedValue() {
var radioBtns = document.getElementsByClassName("radioBtn");
for(var i = 0; i < radioBtns.length; i++){
if(radioBtns[i].checked){
document.getElementById("output").textContent = radioBtns[i].value;
}
}
}
<input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
<input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
<input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
<button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
<textarea id="output"></textarea>
add a comment |
You could do something very similar to Beanz's answer but instead of using IDs, use classes to reduce redundancy.
function getSelectedValue() {
var radioBtns = document.getElementsByClassName("radioBtn");
for(var i = 0; i < radioBtns.length; i++){
if(radioBtns[i].checked){
document.getElementById("output").textContent = radioBtns[i].value;
}
}
}
<input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
<input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
<input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
<button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
<textarea id="output"></textarea>
add a comment |
You could do something very similar to Beanz's answer but instead of using IDs, use classes to reduce redundancy.
function getSelectedValue() {
var radioBtns = document.getElementsByClassName("radioBtn");
for(var i = 0; i < radioBtns.length; i++){
if(radioBtns[i].checked){
document.getElementById("output").textContent = radioBtns[i].value;
}
}
}
<input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
<input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
<input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
<button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
<textarea id="output"></textarea>
You could do something very similar to Beanz's answer but instead of using IDs, use classes to reduce redundancy.
function getSelectedValue() {
var radioBtns = document.getElementsByClassName("radioBtn");
for(var i = 0; i < radioBtns.length; i++){
if(radioBtns[i].checked){
document.getElementById("output").textContent = radioBtns[i].value;
}
}
}
<input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
<input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
<input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
<button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
<textarea id="output"></textarea>
function getSelectedValue() {
var radioBtns = document.getElementsByClassName("radioBtn");
for(var i = 0; i < radioBtns.length; i++){
if(radioBtns[i].checked){
document.getElementById("output").textContent = radioBtns[i].value;
}
}
}
<input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
<input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
<input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
<button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
<textarea id="output"></textarea>
function getSelectedValue() {
var radioBtns = document.getElementsByClassName("radioBtn");
for(var i = 0; i < radioBtns.length; i++){
if(radioBtns[i].checked){
document.getElementById("output").textContent = radioBtns[i].value;
}
}
}
<input class="radioBtn" type="radio" name="order" value="button1" />Button 1<br>
<input class="radioBtn" type="radio" name="order" value="button2" />Button 2<br>
<input class="radioBtn" type="radio" name="order" value="button3" />Button 3<br>
<button onclick="getSelectedValue();">Get Value of Selected Radio</button><br>
<textarea id="output"></textarea>
answered Nov 24 '16 at 23:07
Emily ZhaiEmily Zhai
577
577
add a comment |
add a comment |
all of you can test this example and easy to understand.
Name: <input type="text" id="text" class ="input">
<input type="text" id="text1" class ="input">
Gender: <input type="radio" id="m" class="Rm" name="Rmale" value="Male">
<input type="radio" id="f" class="Rm" name="Rfemale" value="Female">
Course: <input type="checkbox" id="math" class="cm" name="Cmath" value="Math">
<input type="checkbox" id="physic" class="cm" name="Cphysic" value="Physic">
<input type="checkbox" id="eng" class="cm" name="Ceng" value="English">
<button type="button" id="b1">show</button>
// javascript
<script>
function getData(input){
return document.getElementById(input).value;
}
function dataByClassName(st){
var value=document.getElementsByClassName(st)
for(var i=0;i < value.length;i++){
if(value[i].checked){
return value[i].value;
}
}
}
document.getElementById("b1").onclick = function ()
{
var st={
name : getData("text")+getData("text1"),
gender : dataByClassName("Rm"),
course : dataByClassName("cm")
};
alert(st.name+" "+st.gender+" "+st.course);
};
</script>
add a comment |
all of you can test this example and easy to understand.
Name: <input type="text" id="text" class ="input">
<input type="text" id="text1" class ="input">
Gender: <input type="radio" id="m" class="Rm" name="Rmale" value="Male">
<input type="radio" id="f" class="Rm" name="Rfemale" value="Female">
Course: <input type="checkbox" id="math" class="cm" name="Cmath" value="Math">
<input type="checkbox" id="physic" class="cm" name="Cphysic" value="Physic">
<input type="checkbox" id="eng" class="cm" name="Ceng" value="English">
<button type="button" id="b1">show</button>
// javascript
<script>
function getData(input){
return document.getElementById(input).value;
}
function dataByClassName(st){
var value=document.getElementsByClassName(st)
for(var i=0;i < value.length;i++){
if(value[i].checked){
return value[i].value;
}
}
}
document.getElementById("b1").onclick = function ()
{
var st={
name : getData("text")+getData("text1"),
gender : dataByClassName("Rm"),
course : dataByClassName("cm")
};
alert(st.name+" "+st.gender+" "+st.course);
};
</script>
add a comment |
all of you can test this example and easy to understand.
Name: <input type="text" id="text" class ="input">
<input type="text" id="text1" class ="input">
Gender: <input type="radio" id="m" class="Rm" name="Rmale" value="Male">
<input type="radio" id="f" class="Rm" name="Rfemale" value="Female">
Course: <input type="checkbox" id="math" class="cm" name="Cmath" value="Math">
<input type="checkbox" id="physic" class="cm" name="Cphysic" value="Physic">
<input type="checkbox" id="eng" class="cm" name="Ceng" value="English">
<button type="button" id="b1">show</button>
// javascript
<script>
function getData(input){
return document.getElementById(input).value;
}
function dataByClassName(st){
var value=document.getElementsByClassName(st)
for(var i=0;i < value.length;i++){
if(value[i].checked){
return value[i].value;
}
}
}
document.getElementById("b1").onclick = function ()
{
var st={
name : getData("text")+getData("text1"),
gender : dataByClassName("Rm"),
course : dataByClassName("cm")
};
alert(st.name+" "+st.gender+" "+st.course);
};
</script>
all of you can test this example and easy to understand.
Name: <input type="text" id="text" class ="input">
<input type="text" id="text1" class ="input">
Gender: <input type="radio" id="m" class="Rm" name="Rmale" value="Male">
<input type="radio" id="f" class="Rm" name="Rfemale" value="Female">
Course: <input type="checkbox" id="math" class="cm" name="Cmath" value="Math">
<input type="checkbox" id="physic" class="cm" name="Cphysic" value="Physic">
<input type="checkbox" id="eng" class="cm" name="Ceng" value="English">
<button type="button" id="b1">show</button>
// javascript
<script>
function getData(input){
return document.getElementById(input).value;
}
function dataByClassName(st){
var value=document.getElementsByClassName(st)
for(var i=0;i < value.length;i++){
if(value[i].checked){
return value[i].value;
}
}
}
document.getElementById("b1").onclick = function ()
{
var st={
name : getData("text")+getData("text1"),
gender : dataByClassName("Rm"),
course : dataByClassName("cm")
};
alert(st.name+" "+st.gender+" "+st.course);
};
</script>
answered Oct 19 '17 at 16:02
Sophy HengSophy Heng
11
11
add a comment |
add a comment |
Try this, I hope this one will work
function loadRadioButton(objectName, selectedValue) {
var radioButtons = document.getElementsByName(objectName);
if (radioButtons != null) {
for (var radioCount = 0; radioCount < radioButtons.length; radioCount++) {
if (radioButtons[radioCount].value == selectedValue) {
radioButtons[radioCount].checked = true;
}
}
}
}
add a comment |
Try this, I hope this one will work
function loadRadioButton(objectName, selectedValue) {
var radioButtons = document.getElementsByName(objectName);
if (radioButtons != null) {
for (var radioCount = 0; radioCount < radioButtons.length; radioCount++) {
if (radioButtons[radioCount].value == selectedValue) {
radioButtons[radioCount].checked = true;
}
}
}
}
add a comment |
Try this, I hope this one will work
function loadRadioButton(objectName, selectedValue) {
var radioButtons = document.getElementsByName(objectName);
if (radioButtons != null) {
for (var radioCount = 0; radioCount < radioButtons.length; radioCount++) {
if (radioButtons[radioCount].value == selectedValue) {
radioButtons[radioCount].checked = true;
}
}
}
}
Try this, I hope this one will work
function loadRadioButton(objectName, selectedValue) {
var radioButtons = document.getElementsByName(objectName);
if (radioButtons != null) {
for (var radioCount = 0; radioCount < radioButtons.length; radioCount++) {
if (radioButtons[radioCount].value == selectedValue) {
radioButtons[radioCount].checked = true;
}
}
}
}
edited Nov 23 '18 at 9:42
Suraj Rao
23.2k85770
23.2k85770
answered Nov 23 '18 at 9:25
user3585199user3585199
32
32
add a comment |
add a comment |
If you can use jQuery "Chamika Sandamal" answer is the correct way to go. In the case you can't use jQuery you can do something like this:
function selectedRadio() {
var radio = document.getElementsByName('mailCopy');
alert(radio[0].value);
}
Notes:
- In general for the inputs you want to have unique IDs (not a requirement but a good practice)
- All the radio inputs that are from the same group MUST have the same name attribute, for example
- You have to set the value attribute for each input
Here is an example of input radios:
<input type="radio" name="mailCopy" value="1" />1<br />
<input type="radio" name="mailCopy" value="2" />2<br />
add a comment |
If you can use jQuery "Chamika Sandamal" answer is the correct way to go. In the case you can't use jQuery you can do something like this:
function selectedRadio() {
var radio = document.getElementsByName('mailCopy');
alert(radio[0].value);
}
Notes:
- In general for the inputs you want to have unique IDs (not a requirement but a good practice)
- All the radio inputs that are from the same group MUST have the same name attribute, for example
- You have to set the value attribute for each input
Here is an example of input radios:
<input type="radio" name="mailCopy" value="1" />1<br />
<input type="radio" name="mailCopy" value="2" />2<br />
add a comment |
If you can use jQuery "Chamika Sandamal" answer is the correct way to go. In the case you can't use jQuery you can do something like this:
function selectedRadio() {
var radio = document.getElementsByName('mailCopy');
alert(radio[0].value);
}
Notes:
- In general for the inputs you want to have unique IDs (not a requirement but a good practice)
- All the radio inputs that are from the same group MUST have the same name attribute, for example
- You have to set the value attribute for each input
Here is an example of input radios:
<input type="radio" name="mailCopy" value="1" />1<br />
<input type="radio" name="mailCopy" value="2" />2<br />
If you can use jQuery "Chamika Sandamal" answer is the correct way to go. In the case you can't use jQuery you can do something like this:
function selectedRadio() {
var radio = document.getElementsByName('mailCopy');
alert(radio[0].value);
}
Notes:
- In general for the inputs you want to have unique IDs (not a requirement but a good practice)
- All the radio inputs that are from the same group MUST have the same name attribute, for example
- You have to set the value attribute for each input
Here is an example of input radios:
<input type="radio" name="mailCopy" value="1" />1<br />
<input type="radio" name="mailCopy" value="2" />2<br />
answered Jun 20 '13 at 12:51
Alejandro NarancioAlejandro Narancio
7713
7713
add a comment |
add a comment |
var mailcopy = document.getElementById('mailCopy').checked;
if(mailcopy==true)
{
alert("Radio Button Checked");
}
else
{
alert("Radio Button un-Checked");
}
3
The question is looking to get the currently selected button, not find out if a specific button is checked.
– Quentin
Oct 6 '10 at 6:23
add a comment |
var mailcopy = document.getElementById('mailCopy').checked;
if(mailcopy==true)
{
alert("Radio Button Checked");
}
else
{
alert("Radio Button un-Checked");
}
3
The question is looking to get the currently selected button, not find out if a specific button is checked.
– Quentin
Oct 6 '10 at 6:23
add a comment |
var mailcopy = document.getElementById('mailCopy').checked;
if(mailcopy==true)
{
alert("Radio Button Checked");
}
else
{
alert("Radio Button un-Checked");
}
var mailcopy = document.getElementById('mailCopy').checked;
if(mailcopy==true)
{
alert("Radio Button Checked");
}
else
{
alert("Radio Button un-Checked");
}
answered Oct 6 '10 at 5:07
KhanZeeshanKhanZeeshan
94241833
94241833
3
The question is looking to get the currently selected button, not find out if a specific button is checked.
– Quentin
Oct 6 '10 at 6:23
add a comment |
3
The question is looking to get the currently selected button, not find out if a specific button is checked.
– Quentin
Oct 6 '10 at 6:23
3
3
The question is looking to get the currently selected button, not find out if a specific button is checked.
– Quentin
Oct 6 '10 at 6:23
The question is looking to get the currently selected button, not find out if a specific button is checked.
– Quentin
Oct 6 '10 at 6:23
add a comment |
Hy, you have to do it this way.
function checkRadio () {
if(document.getElementById('user1').checked) {
return $('#user1').val();
}else if(document.getElementById('user2').checked) {
return $('#user2').val();
}
}
add a comment |
Hy, you have to do it this way.
function checkRadio () {
if(document.getElementById('user1').checked) {
return $('#user1').val();
}else if(document.getElementById('user2').checked) {
return $('#user2').val();
}
}
add a comment |
Hy, you have to do it this way.
function checkRadio () {
if(document.getElementById('user1').checked) {
return $('#user1').val();
}else if(document.getElementById('user2').checked) {
return $('#user2').val();
}
}
Hy, you have to do it this way.
function checkRadio () {
if(document.getElementById('user1').checked) {
return $('#user1').val();
}else if(document.getElementById('user2').checked) {
return $('#user2').val();
}
}
answered Jul 23 '14 at 8:57
SoftwarerHelpSoftwarerHelp
455
455
add a comment |
add a comment |
Use the element.checked
property.
thank u for your reply . can u explain in clear
– Meena
Oct 6 '10 at 5:01
add a comment |
Use the element.checked
property.
thank u for your reply . can u explain in clear
– Meena
Oct 6 '10 at 5:01
add a comment |
Use the element.checked
property.
Use the element.checked
property.
edited Aug 21 '12 at 9:47
ThePower
11.1k1071116
11.1k1071116
answered Oct 6 '10 at 4:47
Francisco SotoFrancisco Soto
8,61323242
8,61323242
thank u for your reply . can u explain in clear
– Meena
Oct 6 '10 at 5:01
add a comment |
thank u for your reply . can u explain in clear
– Meena
Oct 6 '10 at 5:01
thank u for your reply . can u explain in clear
– Meena
Oct 6 '10 at 5:01
thank u for your reply . can u explain in clear
– Meena
Oct 6 '10 at 5:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3869535%2fhow-to-get-the-selected-radio-button-value-using-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If this method does work already, why're you looking for another?
– Nikita Rybak
Oct 6 '10 at 4:46
2
I think there was a typo that left out a "not"
– Quentin
Oct 6 '10 at 6:28
the question should change to "it does NOT return the selected..."!
– GMsoF
Apr 22 '13 at 6:47
1
Is this still an problem, as you have not accepted an answer!
– pattyd
Jul 22 '13 at 20:36