jquery javascript: take action when radio button is selected
Asked Answered
C

5

12

I have the following radio buttons, none of which is checked by default.

<input type="radio" name="location" value="0" /> home
<input type="radio" name="location" value="1" /> work
<input type="radio" name="location" value="2" /> school

How do I detect when either of them is checked. I'm looking for something like click, but it's not working

$("input[name=location]").click(function(){
    alert("selected");
}); 
Cook answered 7/11, 2009 at 17:27 Comment(0)
P
1

This works fine for me. Are you referencing well the jquery library?

Prod answered 7/11, 2009 at 17:32 Comment(1)
It looks like it was a bug with something above this line, and it messed up the whole javascript below it.Cook
K
14
$('input[name=location]').change(function(){
    alert(this.checked);
});
Killer answered 7/11, 2009 at 17:30 Comment(0)
W
3

Try surrounding the code with below lines

$(document).ready(function(){

}
);
Woof answered 26/3, 2013 at 14:33 Comment(0)
L
2

Have you treid the onChange event?

$("input[name=location]").bind('change', function(){
    alert("selected");
});
Lioness answered 7/11, 2009 at 17:30 Comment(0)
P
1

This works fine for me. Are you referencing well the jquery library?

Prod answered 7/11, 2009 at 17:32 Comment(1)
It looks like it was a bug with something above this line, and it messed up the whole javascript below it.Cook
P
1
$(document).ready(function(){ 
  $("input[name=location]").bind('change', function(){
   alert("Hello!"); 
  }); 
});
Phenica answered 17/12, 2013 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.