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

87
Vistas
How to get data from dynamically generated element using jQuery

I want to do something with data from dynamically generated element, like printing the data from the clicked element, how can I do that?

here's my jquery code:

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);
    });
}

and the transactionHistory data looks like this:

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

and here's the illustration of the dynamically generated element: enter image description here

Basically, I want to get the meterNumber and value from the card I clicked, how can I implement that?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can get desired data by using next() selector or parent() jQuery functions.

Codepen example

using next() function :

$(document).on('click', '.transaction-history-item-image', function() {

    var tranNumber = $(this).next().html();
    var tranValue = $(this).next().next().html();

});

or by using parent() and 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 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