Max Harbarth Max Harbarth - 2 months ago 13
Ajax Question

JQuery on('click') works only once

I know there's a lot of questions like this, but still can't figure it out.

<div class="message"></div>
<button id="getMessage">Get Quote</button>


$.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1", function(data) {
var content = data[0].content + data[0].title;
$("#getMessage").on("click", function() {
$(".message").html(content);
});
});


the problems is that
on("click")
only works once, but I want it to work every time the button is clicked. What am I doing wrong?

Answer

Try this:

$("#getMessage").on("click", function() {
    $.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1", function(data) {
        var content = data[0].content + data[0].title;
        $(".message").html(content);
    });
});
Comments