Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery javascript: take action when radio button is selected

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");
}); 
like image 615
Chris Avatar asked Nov 07 '09 17:11

Chris


People also ask

How to know which radio button is selected using jQuery?

- GeeksforGeeks How to know which radio button is selected using jQuery? To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val () method. This returns the name of the option that is currently selected.

How to retrieve all radio buttons values in JavaScript array?

To retrieve all radio buttons values in JavaScript array use following jQuery code : var values = jQuery ('input:checkbox:checked.group1').map (function () { return this.value; }).get (); Radio buttons are not the same as checkboxes. This example uses checkboxes.

How do you use radio buttons in form input?

The <input> element is used within the <form> tag specifying fields for user input. The type of the field is determined by the value of the type attribute. One of the input types is the radio, which creates a radio button. When one radio button is chosen, all others will be disabled.

How to use jQuery radio button validation in Python?

On clicking the Python radio button, the textbox shows that your selected course is Python. In jQuery radio button validation, we are going apply validation on radio button that it (radio buttons) can't be empty. For example, If any user will not check the radio button then he/she will get the error message.


2 Answers

$('input[name=location]').change(function(){
    alert(this.checked);
});
like image 175
Darin Dimitrov Avatar answered Sep 21 '22 15:09

Darin Dimitrov


Try surrounding the code with below lines

$(document).ready(function(){

}
);
like image 44
Jazin Avatar answered Sep 22 '22 15:09

Jazin