Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda