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

99
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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