-1

I want to get the value of selected radio button.

When no radio is selected it shows undefined correctly. However, when i click on any radio button also it shows undefined.

How can i do this in either javascript or jquery.

HTML code:

<div id="rates">
<input type="radio" class="demo" id="r1" name="rate" value="Fixed Rate"> Fixed Rate
<input type="radio" class= "demo"id="r2" name="rate" value="Variable Rate"> Variable Rate
<input type="radio" class="demo" id="r3" name="rate" value="Multi Rate"> Multi Rate    
</div>  

JS:

console.log($('.demo:selected').val());  

Fiddle: http://jsfiddle.net/eLJ4s/1/

tymeJV
  • 103,943
  • 14
  • 161
  • 157
user3271762
  • 23
  • 1
  • 9
  • Your link is broken unfortunately. Put title in brackets, link in parentheses. – ltalhouarne Feb 21 '14 at 01:02
  • Why would your code do anything other than onload? You've written no code to be triggered on any event click click or change. – j08691 Feb 21 '14 at 01:08
  • Possible duplicate of http://stackoverflow.com/questions/15839169/how-to-get-value-of-selected-radio-button or http://stackoverflow.com/questions/8622336/jquery-get-value-of-selected-radio-button or http://stackoverflow.com/questions/3869535/how-to-get-the-selected-radio-button-value-using-js or http://stackoverflow.com/questions/9806187/get-value-of-checked-radio-button-in-radio-button-list ... and so forth – kiprainey Feb 21 '14 at 01:16

4 Answers4

0

You need to register a change event, your current console.log is only at run-time, when nothing is checked:

$(":radio[name=rate]").change(function() {
    console.log(this.value);
});
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • Paste this code into your code, it'll work. @user3271762 – tymeJV Feb 21 '14 at 01:05
  • Rather than getting the value, how can i find the attribute value.. for example, am using save-id attribute in my code??? how can i get the value of this save-id attribute. Using your answer i done this. However, it shows undefined. $(":radio[name=rate]").change(function() { console.log(this).attr('save-id'); }); – user3271762 Feb 21 '14 at 01:10
0

try this something like this..

$('#element').click(function() {
   if($('#radio_button').is(':checked')) { alert("it's checked"); }
});

You'd have to bind the click event of the checkbox, as the change event doesn't work in IE.

$('#radio_button').click(function(){
    // if ($(this).is(':checked')) alert('is checked'); 
    alert('check-checky-check was changed');
});

Now when you programmatically change the state, you have to trigger this event also:

$('#radio_button').attr("checked", "checked");
$('#radio_button').click();
Kapil
  • 1,823
  • 6
  • 25
  • 47
0

You can change your code accordingly below is the sample

Working samples :

select-a-radio-button-with-jquery

check-if-radio-button-is-checked

Kapil
  • 1,823
  • 6
  • 25
  • 47
0
$(function(){
  $("input[type='radio']").change(function(){
    alert($(this).val());        
  });
});
mikdiet
  • 9,859
  • 8
  • 59
  • 68
harish
  • 295
  • 1
  • 12