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

103
Views
Targeting multi input inside a form for addEventListener

I would like to set up an .addEventListener every time any input bar is selected with a click (focus). I tried to target it with getElementsByTagName('input') but also with querySelectorAll() but it does not work.. The final goal is to print an alert when the field is left empty. many thanks

document.getElementsByTagName('input').addEventListener('focus', function () {
    console.log("on focus")
})
   <form action="" class="signUp">
        <ul>
          <li>
            <!-- <label for="fname"></label> -->
            <input type="text" onfocus="this.value=''" id="fname" name="user_name" placeholder="First Name">
          </li>
          <li>
            <!-- <label for="lname"></label> -->
            <input type="text" onfocus="this.value=''" id="lname" name="user_name" placeholder="Last Name">
          </li>
          <li>
            <!-- <label for="email"></label> -->
            <input type="email" onfocus="this.value=''" id="email" name="user_email" placeholder="Email Address">
          </li>
          <li>
            <!-- <label for="password"></label> -->
            <input type="password" onfocus="this.value=''" id="password" name="password" placeholder="Password">
          </li>
          <li class="button">
            <button type="submit" id="buttonClaim">CLAIM YOUR FREE TRIAL</button>
          </li>
        </ul>
      </form>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

First, you need call getElementsByTagName on document, not document.document. document.document isn't a thing.

Second, getElementsByTagName returns an array of elements. You might notice that Elements is plural here unlike getElementById. With an array, you need to select one element by using an index. Since you want every input to have a listener, you can iterate through the array with a for... of loop.

Finally, the event needs to be lowercase like focus, not Focus

for (let v of document.getElementsByTagName('input')) {
  v.addEventListener('focus', function() {
    console.log("on focus")
  });
}
<form action="" class="signUp">
  <ul>
    <li>
      <!-- <label for="fname"></label> -->
      <input type="text" onfocus="this.value=''" id="fname" name="user_name" placeholder="First Name">
    </li>
    <li>
      <!-- <label for="lname"></label> -->
      <input type="text" onfocus="this.value=''" id="lname" name="user_name" placeholder="Last Name">
    </li>
    <li>
      <!-- <label for="email"></label> -->
      <input type="email" onfocus="this.value=''" id="email" name="user_email" placeholder="Email Address">
    </li>
    <li>
      <!-- <label for="password"></label> -->
      <input type="password" onfocus="this.value=''" id="password" name="password" placeholder="Password">
    </li>
    <li class="button">
      <button type="submit" id="buttonClaim">CLAIM YOUR FREE TRIAL</button>
    </li>
  </ul>
</form>

about 4 years ago · Juan Pablo Isaza Report

0

Simple you can do. you need to explore javascript more.

window.onload = function(){
    var tempobj = document.querySelectorAll('input');
    for(var i=0;i<tempobj.length;i++)
    {
        tempobj[i].addEventListener('focus', function (elem) {
            console.log("on focus: "+elem.target.name);
        });         
    }
}
<form action="" class="signUp">
  <ul>
    <li>
      <!-- <label for="fname"></label> -->
      <input type="text" onfocus="this.value=''" id="fname" name="user_name" placeholder="First Name">
    </li>
    <li>
      <!-- <label for="lname"></label> -->
      <input type="text" onfocus="this.value=''" id="lname" name="user_name" placeholder="Last Name">
    </li>
    <li>
      <!-- <label for="email"></label> -->
      <input type="email" onfocus="this.value=''" id="email" name="user_email" placeholder="Email Address">
    </li>
    <li>
      <!-- <label for="password"></label> -->
      <input type="password" onfocus="this.value=''" id="password" name="password" placeholder="Password">
    </li>
    <li class="button">
      <button type="submit" id="buttonClaim">CLAIM YOUR FREE TRIAL</button>
    </li>
  </ul>
</form>

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!