0

i have 3 set of radio buttons while selecting any one of them i have to get the value of that radio.

iam using following code

Daily<input type="radio" id="rad" value="daily" />
Weekly<input type="radio" id="rad" name="case" value="weekly"/>
None<input type="radio" id="rad" name="case" value="none"/>
alert("rad------"+$('#rad').val());

it alerts the radio button value first time only and multiple radio button seems to be as selected.

Vucko
  • 20,555
  • 10
  • 56
  • 107
Psl
  • 3,830
  • 16
  • 46
  • 84
  • possible duplicate of [jQuery get value of selected radio button](http://stackoverflow.com/questions/8622336/jquery-get-value-of-selected-radio-button) – Ram Oct 03 '13 at 06:51

3 Answers3

3

id of an element must be unique, so in this case you need to find the checked radio button with name case

For that you can use the attribute equals selector along with :checked selector

Daily <input type="radio" id="rad" name="case" value="daily" />
Weekly <input type="radio" id="rad" name="case" value="weekly" />
None <input type="radio" id="rad" name="case" value="none" />

alert("rad------"+$('input[type="radio"][name="case"]:checked').val());
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2

Your problem may be in how you are detecting the change in event, you can do something like this:

$('container input:radio').click(function(){
    alert("rad------"+$(this).val());
});

notes

:radio is short for [type=radio]

substitute container for the container, or use input#radio in the case of your example. Generally it isn't a good idea to assign the same id to a bunch of inputs

Daryl
  • 221
  • 2
  • 5
0

Just to be a pedant - as Arun P Johny wrote IDs must be unique, but in his example he left the duplicate IDs in.

It should be:

Daily <input type="radio" name="case" value="daily" />
Weekly <input type="radio" name="case" value="weekly" />
None <input type="radio" name="case" value="none" />
alert("rad------"+$('input[type="radio"][name="case"]:checked').val());

See Two radio buttons share one "id"? for more details.

Community
  • 1
  • 1
MarkD
  • 33
  • 1
  • 6