I have this button that was created by fetching an object saved in a JSON file. I need to assign a function to it that is activated when clicking on said button. How can I access it? I show the code below.
$.get(URLGET, function (respuesta, estado) {
if (estado === "success") {
let misPromos = respuesta;
for (const promo of misPromos) {
$(".container-cards").append(`<div>
<h3>${promo.promName}</h3>
<p> ${promo.description}</p>
<button class="btn-search danger transparent" id="${promo.id}">Reservar</button>
</div>`);
}
}
// counter= false; // por qué siguen saliendo las cards });
});
You can do this by using su
id
as a jQuery search parameter, and then adding a handler for the "click" event withjQuery.on()
:Or, what is the same, using
jQuery.click()
:So your code would look like the following:
You can also do it without jQuery with the following code using
addEventListener()
:Or also using the property
onclick
:With jquery you can do it like this:
For this we access the document with $('body') and define what we want to hear in this case a click this inside $(document).ready()