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


Solution 1:[1]

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BoBiTza