0

I have a set of radio buttons and one of them is checked like this.

<input name="test_data" type="radio" value='1' id="test_data">
<input name="test_data" type="radio" value='2' id="test_data" checked="checked">
<input name="test_data" type="radio" value='3' id="test_data">
<input name="test_data" type="radio" value='4' id="test_data">

I have a link like this

<a href="#" id="resetvalue">reset value</a>

How can I reset the radio button value when the user clicks on the resetvalue id. I am not using any form. If there is a form and button we can easily do for that using reset property.

Hunter Turner
  • 6,804
  • 11
  • 41
  • 56
sudar
  • 1,446
  • 2
  • 14
  • 26

4 Answers4

2

You could do something like this:

jQuery

$('#resetvalue').click(function() {
  $('input[name="test_data"]:nth-of-type(2)').prop('checked', true);
});

This resets the radio button back to the second value using the :nth-of-type() selector.

JSFiddle

Hunter Turner
  • 6,804
  • 11
  • 41
  • 56
1

This is how you do it https://jsfiddle.net/0vLmt3L5/7/

$('#resetvalue').click(function() {
  $('input[value="2"]').prop('checked', true);
});

This will set it back to the original checked radio button, and yes I took Hunter's fiddle and just added

this:$('input[value="2"]').prop('checked', true);

instead of this: $('input[name="test_data"]').prop('checked', false);

Adam Buchanan Smith
  • 9,422
  • 5
  • 19
  • 39
0
$("#resetvalue").on("click",function(e){

    $("input[type='radio']").removeAttr("checked");

});
Maciej Sikora
  • 19,374
  • 4
  • 49
  • 50
0

$( '#resetvalue' ).click(function() {
    $( '[name="test_data"].checked' ).prop('checked', false);
    // Set eq from 0 (first radio) to value you want
    $( '[name="test_data"]' ).eq(0).prop('checked', true);
    return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="test_data" type="radio" value='1' id="test_data">
    <input name="test_data" type="radio" value='2' id="test_data" checked="checked">
    <input name="test_data" type="radio" value='3' id="test_data">
    <input name="test_data" type="radio" value='4' id="test_data">

<a href="#" id="resetvalue">Reset</a>