1

I'm wasting some hours to find solution, I can't find it...

Here is my code :

$(".usefull").bind("click", function(e){
    e.preventDefault();
    var fbkId = $(this).find(".glyphicon").data("id");
    $(this).parent().html('<a href="#" class="btn btn-grey usefull disabled"><img src="/www/images/mini-loader.gif" alt="" /> Est utile</a>');
    var that = this;
    $.post('{{ route("www-ajax-feedback-add-usefull") }}',{ fbkId: fbkId },
            function(data) {
                if (data == '1') {
                    toastr.success('Merci pour votre contribution !');
                    $(that).parent().html('<i class="glyphicon glyphicon-thumbs-up disabled"></i> Est utile</a>');

                } else {
                    toastr.error(data);
                }
            }
    );
    return false;
});

When I click on a button, I update the button with loading gif.

When my ajax call is finish, I want to update the state button, but it's not working.

Thanks for help

Here it is more informations :

<a href="#" class="btn btn-grey agree"><i class="glyphicon glyphicon-thumbs-up" data-id="2161"></i> Je suis d'accord</a>

If I put console.log($(that).html()); after $.post here it output : <i class="glyphicon glyphicon-thumbs-up" data-id="2161"></i> Je suis d'accord

sHaDeoNeR
  • 617
  • 2
  • 8
  • 14

2 Answers2

0

For any elements which will get binding to the DOM through ajax or added at run time, you have to use

http://api.jquery.com/live/

or delegates

http://api.jquery.com/delegate/

Those will be binded at the document level.

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
0

==>this reference a javascript object and $(this) used to encapsulate with jQuery.

$(".usefull").bind("click", function(e){
    e.preventDefault();
    var fbkId = $(this).find(".glyphicon").data("id");
    $(this).parent().html('<a href="#" class="btn btn-grey usefull disabled"><img src="/www/images/mini-loader.gif" alt="" /> Est utile</a>');
    var that = $(this);
   // use $(this); insetd of this;

    $.post('{{ route("www-ajax-feedback-add-usefull") }}',{ fbkId: fbkId },
            function(data) {
                if (data == '1') {
                    toastr.success('Merci pour votre contribution !');
                    $(that).parent().html('<i class="glyphicon glyphicon-thumbs-up disabled"></i> Est utile</a>');

                } else {
                    toastr.error(data);
                }
            }
    );
    return false;
});

read more at jQuery: What's the difference between '$(this)' and 'this'?

Community
  • 1
  • 1
Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49