How do I get the value of the selected radio button?

  • 0
    Suppose there are two radio buttons, how to display the value of the selected radio button in console.log () ?
    <div>
      <input type="radio" name="gender" value="Человек">
      <input type="radio" name="gender" value="Женщина">
      <button>Отправить</button>
    </div>

    P.S: In general, if you google this question, then the solutions there are some ugly, therefore
    I asked this question, suddenly there is a way to do it somehow elegantly. Clean JS is desirable.
    JavaScript Aubrey Orr, May 2, 2019

  • 2 Answers
  • 0
    document.querySelector('button').addEventListener('click', function() {
    console.log(document.querySelector('[type="radio"]:checked').value);
    });
    Victoria Olsen

  • 0
    Instead of a diva, see the formData values ​​for the submission event
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!