javascript – How to check another radio button if a certain one is already checked

Question:

<div>
    <input type="radio" name="option" id="radio2" />
    <label for="radio2">radio кнопка №2</label>
</div>
<div>
    <input type="radio" name="anothername" id="radio3" />
    <label for="radio3">Еще текст radio кнопки №3</label>
</div>
<div>
    <input type="radio" name="anothername" id="radio4" />
    <label for="radio4">Еще текст radio кнопки №4</label>
</div>

how to write a condition if a radio button with name = "option" id = "radio2" is checked, then check name = "anothername" with id = "radio4" too

Answer:

jquery has an is method. We use it to define check or uncheck buttons. And after that, using the prop method, we change the check another radio button.

<!-- begin snippet: js hide: false console: true babel: false -->
$(document).ready(function(){
  $('#radio2').change(function(event){
    if($(this).is(':checked')){
      $( "#radio4" ).prop( "checked", true );
      
      //После того как мы сделали `check` Второй радио кнопки давайте сделаем лог.
      console.log($( "#radio4" ).is(':checked'));
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <input type="radio" name="option" id="radio2" />
    <label for="radio2">radio кнопка №2</label>
</div>
<div>
    <input type="radio" name="anothername" id="radio3" />
    <label for="radio3">Еще текст radio кнопки №3</label>
</div>
<div>
    <input type="radio" name="anothername" id="radio4" />
    <label for="radio4">Еще текст radio кнопки №4</label>
</div>
Scroll to Top