user7167236 user7167236 - 6 days ago 6
Javascript Question

How to modify clickable elements in jQuery?

Why doesn't the on click listener work after clicking on the first list-button?

JSFiddle link

$(".acceptTask").on("click", function(){
acceptTask(this);
});

$(".solveTask").on("click", function() {
solveTask(this);
});

function solveTask(e){
...
}

function acceptTask(e){
...
$(document).on("click", ".solveTask", solveTask);
}

Answer

$('.solveTask').on('click', /*...*/) only applies the event handler to anything that has a class "solveTask" at that time. So when you add the solveTask class in your acceptTask function, add an event listener.

$(e).addClass('btn-warning solveTask')
    .click(function () { solveTask(this); });

See fiddle: https://jsfiddle.net/1203y34b/1/

Comments