How do I put a required input without the tag form?

Asked

Viewed 15 times

-2

Whenever I put the required property, my scrip stops working.

How can I insert a required input without the tag form?

var cont1 = 1;
var cont2 = 2;
var cont3 = 3;

document.getElementById("btnSubmit").onclick = function() {
  var radios = document.getElementsByName("nome-radio");
  for (var i = 0; i < radios.length; i++) {

    if (radios[i].checked) {
      console.log("Escolheu: " + radios[i].value);
      if (radios[i].value == cont1) {
        console.log("ola 1");
      } else if (radios[i].value == cont2) {
        console.log("ola 2");
      } else if (radios[i].value == cont3) {
        console.log("ola 3");
      }

    }
  }
}
<html lang="pt-br">

<head>
  <title>Exemplo</title>
  <meta charset="utf-8">
</head>

<body>
  <form>
    <p>
      <input type="radio" name="nome-radio" value=1>Teste 1
      <input type="radio" name="nome-radio" value=2>Teste 2
      <input type="radio" name="nome-radio" value=3>Teste 3
    </p>
    <p>
      <input type="button" id="btnSubmit" value="Entrar" />

    </p>
  </form>

</body>

</html>

1 answer

0

Well I tested in codepen and your code has no problem just put the required in one of the radio-Buttons since all the "name" has equal values just add the required in only one element:

https://codepen.io/lucasbpereira/pen/wvgmxGO
<html lang="pt-br">

<head>
  <title>Exemplo</title>
  <meta charset="utf-8">
</head>

<body>
  <form>
    <p>
      <input type="radio" name="nome-radio" value=1 required>Teste 1
      <input type="radio" name="nome-radio" value=2>Teste 2
      <input type="radio" name="nome-radio" value=3>Teste 3
    </p>
    <p>
      <input type="button" id="btnSubmit" value="Entrar" />

    </p>
  </form>

</body>

</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.