JSFiddle - React, Tailwind, and code Playground

by techiedelight

HTML

<html>
  <body>
    <div id="container">
      <p>Select your preferred contact method:</p>
      <div>
        <input type="radio" id="email" name="contact" value="email">
        <label for="email">Email</label>

        <input type="radio" id="phone" name="contact" value="phone">
        <label for="phone">Phone</label>

        <input type="radio" id="mail" name="contact" value="mail">
        <label for="mail">Mail</label>
      </div>
      <button id="submit">Submit</button>
    </div>
  </body>
</html>

CSS

p,
label {
  font: 15px 'Exo 2', sans-serif;
}

input {
  margin: 6px;
}

button {
  margin-top: 10px;
  padding: 2 2;
  font: 15px 'Exo 2', sans-serif;
}

JavaScript

document.getElementById('submit').onclick = function() {
  var selected = false;
  var radios = document.getElementsByName('contact');
  for (var radio of radios) {
    if (radio.type === 'radio' && radio.checked) {
      alert('Thank you for submitting your preference.');
      selected = true;
    }
  }

  if (!selected) {
    alert('Please select your preferred contact method!');
  }
}