Uncheck all JQuery radio buttonset at once

Viewed 42804

Using jQ-ui's buttonset feature

<script>
    $(function() {
        $( "#radio" ).buttonset();
    });
    </script>


    <div id="radio">
        <input type="radio" id="radio1" name="radio" /><label for="radio1">Choice 1</label>
        <input type="radio" id="radio2" name="radio" checked="checked" /><label for="radio2">Choice 2</label>
        <input type="radio" id="radio3" name="radio" /><label for="radio3">Choice 3</label>
    </div>

Is there any way to uncheck all radio buttons of buttonset at once?

7 Answers

For JQuery 1.12+

Wrap the radio buttons within a <div> and give it an id (e.g. "radio")

$("#radio").find("input:radio").prop("checked", false)

Javascirpt native way to achieve this

function reset(){
//var list = document.querySelectorAll('input[type=radio]');
var list =document.querySelectorAll('input[type="radio"]:checked')
debugger
list.forEach(element => { 
if(element.checked){element.checked=false}}
    
);
}
<div id="myDiv" class="myclass">
    <input type="radio" name="myname" id="id1"  value="1"><label for="id1">Label1</label>
    <input type="radio" name="myname" id="id2" value="2"><label for="id2">Label2</label>
    <input type="radio" name="myname" id="id3" checked value="3"><label for="id3">Label3</label>
</div>

<button onclick="reset()">reset me</button>

Related