Search code examples
jqueryselectoronchange

how to set radio button same value if same name attr


how to set same value when radio button onchange(when select someone radiobutton)

<body>

    <label>slect color:</label>
    <br>
    <label><input type="radio" name="logic_01">YES</label>
    <label><input type="radio" name="logic_01">NO</label>
    <br>
    <label><input type="radio" name="logic_02">YES</label>
    <label><input type="radio" name="logic_02">NO</label>
    <br>
    <label><input type="radio" name="logic_03">YES</label>
    <label><input type="radio" name="logic_03">NO</label>
<script>
    $('input[type=radio]').change(function(){
        $(this).attr("value","SS")
    })     
</script>    

</body>

for example select name logic_01 then

<label><input type="radio" name="logic_01" value="logic_01">YES</label>
<label><input type="radio" name="logic_01" value="logic_01>NO</label>

Solution

  • Use an attribute selector that selects all radio buttons with the current name.

    $('input[type=radio]').change(function(){
        $(`:radio[name=${this.name}]`).val("SS");
    });