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

131
Visualizações
Target Input and nextElementSibling With forEach Loop / event.Target Property - JavaScript

I have a series of forms on a page which each have a simple text counter on the input element (to illustrate the length of a title as it is being typed). When I was working out how to do the counter it was on a singular instance of the form.

How do I have it so the counter works in relation to the nextElementSibling when there are multiple instances of the form? I would've thought it would be done with e.target property, but I can't work out how to store the input element as a target so to speak? I thought e.target = item in the code below would work but this doesn't.

Codepen: https://codepen.io/thechewy/pen/powZppR

var title = document.querySelectorAll(".image-title-upload"),
  charsRemaining = document.querySelectorAll(".image-title-upload").nextElementSibling,
  maxValue = 125;

title.forEach((item) => {
  item.addEventListener("input", (e) => {
    
    //e.target = item; thought this might work but it doesn't
    
    remaining = maxValue - item.value.length; // work out how many characters are left

    charsRemaining.textContent = remaining;
  });
});
form {
  margin-bottom: 2rem;
}

span {
  display: block;
  margin-top: 4px;
}
<form>
  <input class="image-title-upload" type="text" name="image-title" placeholder="Title">
  <span class="tl characters-remaining">125</span>
</form>

<form>
  <input class="image-title-upload" type="text" name="image-title" placeholder="Title">
  <span class="tl characters-remaining">125</span>
</form>

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

0

this way

const 
  titleInput = document.querySelectorAll(".image-title-upload") 
, maxValue   = 125;

titleInput.forEach( item  =>
  {
  item.oninput = e =>
    {
    item.nextElementSibling.textContent = maxValue - item.value.length
    }
  })
form {
  margin-bottom: 2rem;
}

span {
  display: block;
  margin-top: 4px;
}
<form>
  <input class="image-title-upload" type="text" name="image-title" placeholder="Title">
  <span class="tl characters-remaining">125</span>
</form>

<form>
  <input class="image-title-upload" type="text" name="image-title" placeholder="Title">
  <span class="tl characters-remaining">125</span>
</form>

you can also use

e.target.closest('form').querySelector('.characters-remaining').textContent = ....
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