Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda