listen to Click event on dynamically added elements
I dynamically add some elements to an existing element, but I can't get the Click event to work on the dynamically added elements.
I've worked with similarly scenarios before and using ON has always worked, but not now.
Function:
- function SetStars(count) {
- console.log("SetStars (" + count + ")");
- var $stars = $createWrap.find(".score-wrap > .stars");
- $stars.empty();
- for (let i = 1; i <= 5; i++) {
- let $elm = document.createElement("i");
- $($elm).addClass("fa-star").addClass("star").addClass("clickable");
- $($elm).data("count", i);
- if (i <= count) {
- $($elm).addClass("fas");
- }
- else {
- $($elm).addClass("far");
- }
- $stars.append($elm);
- }
- }
Event listener:
- $($createWrap).find(".star.clickable").on("click", function () {
- console.log("click");
- var count = parseInt($(this).data("count"));
- SetStars(count);
- });
----------
Just so we're clear - you need a hero?