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

91
Views
Cómo obtener datos de un elemento generado dinámicamente usando jQuery

Quiero hacer algo con los datos del elemento generado dinámicamente, como imprimir los datos del elemento en el que se hizo clic, ¿cómo puedo hacer eso?

aquí está mi código jquery:

 let setHistory = () => { $('#transaction-history').empty(); transactionHistory.forEach((o) => { var htmlBlock = ""; htmlBlock += `<div class="btn btn-outline transaction-history-item d-flex flex-column justify-content-center mr-4">` + `<img class="transaction-history-item-image card pl-2 pr-2 pt-1 pb-1 mx-auto" src="../assets/placeholder-image.png" alt="" >` + `<div class="transaction-history-number mt-1 mx-auto">${o.meterNumber}</div>` + `<div class="transaction-history-value mx-auto" data-value=${o.value}>Rp${o.value.toLocaleString().replaceAll(',','.')}</div>` + `</div>`; $('#transaction-history').append(htmlBlock); }); }

y los datos del historial de transacciones se ven así:

 let transactionHistory = [ { meterNumber: "1234152322", value: 250000 }, { meterNumber: "1115648463", value: 50000 } ]

y aquí está la ilustración del elemento generado dinámicamente: ingrese la descripción de la imagen aquí

Básicamente, quiero obtener el número de medidor y el valor de la tarjeta en la que hice clic, ¿cómo puedo implementar eso?

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

0

Puede obtener los datos deseados utilizando las funciones next() selector o parent() jQuery.

Ejemplo de código abierto

usando la función next() :

 $(document).on('click', '.transaction-history-item-image', function() { var tranNumber = $(this).next().html(); var tranValue = $(this).next().next().html(); });

o usando parent() y find() :

 $(document).on('click', '.transaction-history-item-image', function() { var tranNumber = $(this).parent().find('.transaction-history-number').html(); var tranValue = $(this).parent().find('.transaction-history-value').html(); });
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!