Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
Obtener ID de la imagen anterior en el evento de clic

Tengo este código HTML

 <div> <img src="download1.jpeg" image-id="9a8ae555af5f"></div> <div><h1><a href ="#">test</a></h1></div>

Cuando un usuario hace clic en el enlace de "prueba", me gustaría navegar por el árbol y buscar el atributo "id de imagen" de la imagen que precede al enlace.

He intentado esto pero imageID devuelve indefinido

 function trackClick(event) { let imageId = this.$('a').prev('img').attr('image-id'); console.log("imageId", imageId); } document.addEventListener('click', trackClick, true);

¿Alguna idea sobre cómo puedo hacer que esto funcione usando los selectores js o css?

Gracias

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Esta es la solución jquery con la función prev() y parent():

 $('#test').on('click', function() { alert($(this).parent().parent().prev('div').find('img').attr('image-id')) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <img src="download1.jpeg" image-id="9a8ae555af5f"> </div> <div> <h1> <a href ="#" id = 'test'>test</a> </h1> </div>

about 4 years ago · Juan Pablo Isaza Report

0

Usando Vanilla JS, paso a paso:

 function trackClick(event) { let targetElement = event.target || event.srcElement; let closestDiv = targetElement.closest('div'); let previousDiv = closestDiv.previousElementSibling; let insideImage = previousDiv.getElementsByTagName('img'); // or // let insideImage = previousDiv.children; console.log(insideImage[0].getAttribute('src')); } document.addEventListener('click', trackClick, true);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!