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

156
Visualizações
Find Closest element with same class

I'm searching to have the closest element address-department:

<div>
  <div class="field">
    <input type="text" class="address-zipcode">
  </div>
  <div class="field">
    <input type="text" class="address-department">
  </div>
  <div class="field">
    <input type="text" class="city">
  </div>
  <div class="field">
    <input type="text" class="country">
  </div>
</div>

<div>
  <div class="field">
    <input type="text" class="address-zipcode">
  </div>
  <div class="field">
    <input type="text" class="address-department">
  </div>
  <div class="field">
    <input type="text" class="city">
  </div>
  <div class="field">
    <input type="text" class="country">
  </div>
</div>

I'm doing ajax, when i tape on zip-code then I autocomplete address-department, but now I have 2 blocks address and I want when I "input" on address-zipcode then he changes only the closest address-department.

I tried with this.closest('.field').closest('.field.address-department') but it's not working. Do you know how I can do that ? Thank !

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

0

I agree that the naming closest is a bit confusing. you can think of closest as a reverse querySelector. It will basicly find the next parent element that matches the statement. so you kind of can go down but not up again with that.

I would suggest just using querySelector for this. This would be independent of structure, order and depth.

Basicly: move back to a common base (in this case a surrounding form) and search the element.

You can find this common base by calling closest('your-base') or by accessing if via ID, or whatever.

const button = document.getElementById('button');
const button2 = document.getElementById('button2');

button.addEventListener('click', () => {
  const form = document.getElementById('base');
  
  const department = form.querySelector('.address-department');
  if(!department) return;
  
  department.classList.toggle('found');
})

button2.addEventListener('click', () => {
  const form = document.getElementById('search-start');
  
  const department = form.closest('form').querySelector('.address-department');
  if(!department) return;
  
  department.classList.toggle('found');
})
.found {
  background: red;
}
<form id="base">
  <div>
    <div class="field">
        <input type="text" class="address-zipcode" id="search-start">
    </div>
    <div class="field">
        <input type="text" class="address-department">
    </div>
    <div class="field">
        <input type="text" class="city">
    </div>
    <div class="field">
        <input type="text" class="country">
    </div>
  </div>

  <div>
    <div class="field">
        <input type="text" class="address-zipcode">
    </div>
    <div class="field">
        <input type="text" class="address-department">
    </div>
    <div class="field">
        <input type="text" class="city">
    </div>
    <div class="field">
        <input type="text" class="country">
    </div>
  </div>
</form>

<button id="button">Find Department from Base</button>
<button id="button2">Find Department from Input</button>

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