Как проверить нажата ли радио-кнопка JS

нужно проверить нажата ли определённая радио-кнопка


Ответы (2 шт):

Автор решения: DiD

document.querySelector('button').onclick=(e)=>
  console.log(document.querySelectorAll('input')[0].matches(':checked'),
  document.querySelectorAll('input')[1].matches(':checked'));
<input type="radio" name="x"/><input type="radio" name="x"/><button>Check</button>

→ Ссылка
Автор решения: Aziz Umarov

Вот пример на JS отсюда

function myFunction() {
  var coffee = document.getElementsByName("coffee");
  var txt = "";
  var i;
  for (i = 0; i < coffee.length; i++) {
    if (coffee[i].checked) {
      txt = txt + coffee[i].value + " ";
    }
  }
  document.getElementById("order").value = "You ordered a coffee with: " + txt;
}
<input type="radio" name="coffee" value="cream">With cream<br>
<input type="radio" name="coffee" value="sugar">With sugar<br>

<input type="text" id="order" size="50">
<input type="button" onclick="myFunction()" value="Send order">

→ Ссылка