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");
}); 

5 Answers 5

14
$('input[name=location]').change(function(){
    alert(this.checked);
});
Sign up to request clarification or add additional context in comments.

Comments

3

Try surrounding the code with below lines

$(document).ready(function(){

}
);

Comments

2

Have you treid the onChange event?

$("input[name=location]").bind('change', function(){
    alert("selected");
});

Comments

1

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

1 Comment

It looks like it was a bug with something above this line, and it messed up the whole javascript below it.
1
$(document).ready(function(){ 
  $("input[name=location]").bind('change', function(){
   alert("Hello!"); 
  }); 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.