Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio button checked event handling

In my application, I need a radio group, in which whenever a radio-button is checked, an alert occur so that I can post it's value to ajax post with jQuery.

Can you help me please how i can do it in jQuery?

like image 844
tarique Avatar asked Apr 28 '10 17:04

tarique


People also ask

Does checked work for radio buttons?

If a radio button is checked, its checked property is true . Then, we assign the value of the selected radio button to the selectedSize variable. Since only one radio button in a radio group can be checked at a time, the loop is terminated immediately by the break statement.

What are the events for radio button?

A radio button fires the change event after you select it. How it works: First, register an event handler to the change event of the body . When a radio button is clicked, its change event is bubbled to the body.

How do you check if radio button is checked or not in JavaScript?

Use document. getElementById('id'). checked method to check whether the element with selected id is check or not. If it is checked then display its corresponding result otherwise check the next statement.


4 Answers

Try something like this:

$(function(){   $('input[type="radio"]').click(function(){     if ($(this).is(':checked'))     {       alert($(this).val());     }   }); }); 

If you give your radio buttons a class then you can replace the code $('input[type="radio"]') with $('.someclass').

like image 127
Sarfraz Avatar answered Sep 20 '22 08:09

Sarfraz


Update in 2017: Hey. This is a terrible answer. Don't use it. Back in the old days this type of jQuery use was common. And it probably worked back then. Just read it, realize it's terrible, then move on (or downvote or, whatever) to one of the other answers that are better for today's jQuery.


$("input[type=radio]").change(function(){     alert( $("input[type=radio][name="+ this.name + "]").val() ); }); 
like image 24
David Murdoch Avatar answered Sep 21 '22 08:09

David Murdoch


The HTML code:

<input type="radio" name="theName" value="1" id="option-1">
<input type="radio" name="theName" value="2">
<input type="radio" name="theName" value="3">

The Javascript code:

$(document).ready(function(){
    $('input[name="theName"]').change(function(){
        if($('#option-1').prop('checked')){
            alert('Option 1 is checked!');
        }else{
            alert('Option 1 is unchecked!');
        }
    });
});

In multiple radio with name "theName", detect when option 1 is checked or unchecked. Works in all situations: on click control, use the keyboard, use joystick, automatic change the values from other dinamicaly function, etc.

like image 37
e-info128 Avatar answered Sep 20 '22 08:09

e-info128


You can simply use the method change of JQuery to get the value of the current radio checked with the following code:

$(document).on('change', '[type="radio"]', function() {
    var currentlyValue = $(this).val(); // Get the radio checked value
        
    alert('Currently value: '+currentlyValue); // Show a alert with the current value
});

You can change the selector '[type="radio"]' for a class or id that you want.

like image 25
Radames E. Hernandez Avatar answered Sep 20 '22 08:09

Radames E. Hernandez