Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
Constraint Validation API: Questions Regarding Element Selector

What exactly is the [0] in this bit of code?

Why is it there?

What does it do?

Thank you!

const form  = document.getElementsByTagName('form')[0];


<form novalidate>
    <p>
      <label for="mail">
        <span>Please enter an email address:</span>
        <input type="email" id="mail" name="mail" required minlength="8">
        <span class="error" aria-live="polite"></span>
      </label>
    </p>
    <button>Submit</button>
  </form>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

const form = document.getElementsByTagName('form')[0];

The form constant is using the "Get Elements By Tag Name" method for all of the "Form" tags in the document. Since this returns an array of elements, the [0] is specifying the element at Index 0 of the array.

In this case, it is only saving the first array element as the Form constant. Depending on whatever other code there is, it could be used to quickly refer to the HTML form instead of running a document query each time you want to use it.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda