35

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?

skobaljic
  • 9,379
  • 1
  • 25
  • 51
tarique
  • 2,201
  • 5
  • 19
  • 28

6 Answers6

58

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').

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 4
    @David Murdoch: yeah that's right, you could use `change` though. – Sarfraz Apr 28 '10 at 17:43
  • 1
    $.click() does not call when use the keyboard or other control to manipulate the form controls. Click function does not works in all situations. – e-info128 Feb 25 '17 at 00:34
13

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() );
});
David Murdoch
  • 87,823
  • 39
  • 148
  • 191
  • 4
    Can you just do $(this).val()? – n s Mar 04 '14 at 19:17
  • 1
    It took me a while to understand what ```$("input[type=radio][name="+ this.name + "]").val()``` is doing here. Can you please edit your post and change it to ```alert( $(this).val() );``` I suppose that was a reason of down votes. – Andrew Surzhynskyi Jun 07 '14 at 09:20
  • @Applejack : do not change something that is correct, just add another. This is a better answer than `alert( $(this).val() );` – jimjim Nov 08 '16 at 03:52
  • $.change() does not call when uncheck the radio. This function is realy onCheck. – e-info128 Feb 25 '17 at 00:31
  • Agree with Applejack, answer should be changed (or downvoted into oblivion because it is wrong ;-) ). See: https://plnkr.co/edit/563jmzFvukpqHlA0HnvC?p=preview . It will trigger when the check-change occurs, but it will always result in the same value. – Adam Plocher Mar 07 '17 at 00:45
  • @e-info128 unchecking a radio isn't supposed to be a thing... that sorta defeats the purpose of the radio button. – Adam Plocher Mar 07 '17 at 00:49
6

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.

e-info128
  • 3,727
  • 10
  • 40
  • 57
1

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.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Radames E. Hernandez
  • 4,235
  • 27
  • 37
0
$("#expires1").click(function(){
     if (this.checked)
        alert("testing....");
});
Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
  • however correct this may be, without an explanation as to why it's the answer it's not going to teach anything. And with the question in question over 8 years old, that doesn't make it at all helpful. – jwenting Aug 22 '18 at 13:49
0
$(document).on('change','.radio-button', function(){

    const radio = $(this);

    if (radio.is(':checked')) {
      console.log(radio)
    }

});
Lu Blue
  • 335
  • 3
  • 10