Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use on click do not work after ajax call

 $(document).ready(function () {
var user = 1;
 $("a.like").on("click", function () {
            var article = $(this).attr('data-article');
            $.ajax({
                type: "POST",
                url: "WebService.asmx/Like",
                data: "{ 'user': '" + user + "', 'article': '" + article + "' }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    if(msg.d ==  1)
                    {
                        $(".like[data-article='" + article + "']").fadeOut(700).addClass('unlike').removeClass('like').stop().text('Unlike').fadeIn(700);
                    }
                },
                failure: function (msg) {
                    alert(msg.d);
                }
            });
        });

$("a.unlike").on("click", function () {
            var article = $(this).attr('data-article');
            $.ajax({
                type: "POST",
                url: "WebService.asmx/Unlike",
                data: "{ 'user': '" + user + "', 'article': '" + article + "' }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    if(msg.d ==  1)
                    {
                        $(".unlike[data-article='" + article + "']").fadeOut(700).addClass('like').removeClass('unlike').stop().text('Like').fadeIn(700);
                    }

                },
                failure: function (msg) {
                    alert(msg.d);
                }
            });
        });
});

When I click on a link with class like it's working fine, the same when I click on a link with class unlike. However it doesn't working when I click the same link twice! When I'm searching on the internet, I see that I have to use live instead of click, however live was deprecated on 1.8 version. How can I make this fires multiple times?

like image 371
samuk Avatar asked Jan 30 '26 16:01

samuk


2 Answers

Use jQuery.on():

$( document ).on( 'click', 'a.like', function () {
    // Do click stuff here
} );

http://api.jquery.com/on/

You need to bind on to an element that will always be present. (document or some main wrapper div). Using on with the dynamic element will not work.

like image 162
Kevin Boucher Avatar answered Feb 02 '26 05:02

Kevin Boucher


If you want to use on instead of live you need to write something like

$('#container').on('click', '.a.unlike', function () {

Since

$("a.like").on("click", function () {

Worked just as bind() did before it was deprecated.

If you want on() to act as live() you need to pass 3 arguments as in my first example.

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. For help in converting from older jQuery event methods, see .bind(), .delegate(), and .live(). To remove events bound with .on(), see .off(). To attach an event that runs only once and then removes itself, see .one()

like image 20
Johan Avatar answered Feb 02 '26 06:02

Johan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!