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

165
Visualizações
How can I select multiple span elements?

If I select my span element by ID it gets executed but I want to select multiple span elements. I tried with class and name but it also not working.

const uname = document.getElementById('fname')
const form = document.getElementById('form')
const errorElement = document.getElementById('formerror')

form.addEventListener('submit', (e) = \ > {
  let messages = \ [\]

  if (uname.value === '' || uname.value == null) {
    messages.push("Name is required")
  }

  if (messages.length\ > 0) {
    e.preventDefault()
    errorElement.innerHTML = messages.join(', ')
  }
})
<div id="error">
  <form name="signupform" action="signupdetails.php" method="get" id="form">
    <input type="text" class="text" placeholder="Username" id="fname"><b><span id="formerror"></span></b>
    <input type="text" class="text" placeholder="Roll:no" name="froll"><b><span id="formerror"></span></b>
    <input type="email" class="text" placeholder="Email" id="femail"><b><span id="formerror"></span></b>
    <i class="fa fa-eye-slash" aria-hidden="true" id="icon1"></i>
    <input type="password" class="text" placeholder="Password" id="password1" name="fpass">
    <i class="fa fa-eye-slash" aria-hidden="true" id="icon2"></i>
    <input type="password" class="text" placeholder="Confirm Password" id="password2" name="fcpass">
    <button id="btn" style="color: white;" type="submit">Sign up</button> <br> <br> <br>
  </form>
</div>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

An id should be unique in the entire document, see https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute

Use a class on your span elements and query them with document.querySelectorAll('.formerror').

const uname = document.getElementById('fname')
const form = document.getElementById('form')
const errorElements = document.querySelectorAll('.formerror')

console.log(errorElements)
<div id="error">
  <form name="signupform" action="signupdetails.php" method="get" id="form">
    <input type="text" class="text" placeholder="Username" id="fname"><b><span class="formerror"></span></b>
    <input type="text" class="text" placeholder="Roll:no" name="froll"><b><span class="formerror"></span></b>
    <input type="email" class="text" placeholder="Email" id="femail"><b><span class="formerror"></span></b>
    <i class="fa fa-eye-slash" aria-hidden="true" id="icon1"></i>
    <input type="password" class="text" placeholder="Password" id="password1" name="fpass">
    <i class="fa fa-eye-slash" aria-hidden="true" id="icon2"></i>
    <input type="password" class="text" placeholder="Confirm Password" id="password2" name="fcpass">
    <button id="btn" style="color: white;" type="submit">Sign up</button> <br> <br> <br>
  </form>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Just had a somewhat similar issue to this, & as others have already mentioned. You will want to change this snippet of code below in you're javascript

document.getElementById('formerror')

To instead

document.querySelectorAll(".formerror") // Be sure to also change from and ID to instead using classes for the form error

Also as ID's are only meant to be used once, trying to assign them and use them more than once should not work as they are meant to be unique to one element. So this is why you should instead transition to adding them as classes instead.

Now as for when you are trying to access you're span elements by Javascript, since there is more than one span element. You will need to ensure that you use the "querySelectorAll" function as this will allow you to target all of you're span elements. Otherwise if using just the "querySelector" it will only apply to the first span element, while the other two span elements remain un affected.

Hope this could help to add a bit of insight and clear some things up for you.

about 4 years ago · Juan Pablo Isaza Relatório

0

First change the id to a class attribute, because the id should be unique to the entire document.

<span class="formerror"></span>

Then Instead of using document.getElementById(), use document.querySelectorAll(".formerror").

It will solve your problem.

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