emurb
emurb

Reputation: 147

Jquery ajax post request is sent just one time on a click action

I have a button :

<a href="#" id="buttonNew" class="buttonNew">{{ 'buttonNewText'|trans }}</a>

And an ajax action when i click on this button :

    $(function(){
        $("#buttonNew").click(function(e){
            $.ajax({
                type: "POST",
                url: "{{ path('ajax_load_new_note_form') }}",
                success: function(data) {
                    $('#homeLeft').html(data);
                }
            });
        });
    });

The first time i click on my button, a post request is sent, so it's OK. But when i click again no other request is sent. When i reload my page, the click send the request and not the next clicks.

What am i missing ?

Thank you by advance

Upvotes: 1

Views: 1648

Answers (2)

salih0vicX
salih0vicX

Reputation: 1373

This should work:

$(function(){
    $("#buttonNew").click(function(e){
        $.ajax({
            type: "POST",
            url: "{{ path('ajax_load_new_note_form') }}",
            success: function(data) {
                $(this).html(data);
            }
        });
    });
});

Upvotes: 0

KoU_warch
KoU_warch

Reputation: 2150

try

$(function(){
    $("body").on('click','#buttonNew',function(e){
        $.ajax({
            type: "POST",
            url: "{{ path('ajax_load_new_note_form') }}",
            success: function(data) {
                $('#homeLeft').html(data);
            }
        });
    });
});

Upvotes: 2

Related Questions