Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!