fightstarr20
fightstarr20

Reputation: 12598

jQuery change default radio button value

I am trying to get the radio button that originally had checked against it by default and reset the value of it.

In the example below the value is already set to

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="radio" name="test" value="0" checked>
<input type="radio" name="test" value="1">
<input type="radio" name="test" value="2">

I am using this to select the empty value, but how can I set it to empty first before selecting it?

$('input[name="test"][value=""]').prop('checked', true);

Upvotes: 0

Views: 115

Answers (2)

BoBiTza
BoBiTza

Reputation: 98

You can make an .each on inputs and check if value is empty example:

$('input[name="test"]').each(function() {
     if($(this).val() === '') {
          //do something
     }
});

Upvotes: 1

Nitheesh
Nitheesh

Reputation: 19986

Select the checked input using $('input[name="test"][checked]') and update its value with .val("")

const checkedInput = $('input[name="test"][checked]');
checkedInput.val("");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="radio" name="test" value="0" checked>
<input type="radio" name="test" value="1">
<input type="radio" name="test" value="2">

Upvotes: 1

Related Questions