6

I've got this code in twig

 {% if followsId == null %}
            <div id="followUser" class="follow" data-userId="{{ profileUserData.id }}" data-currentUserId="{{ loggedUserData.id }}" data-action="follow">
                Follow
            </div>
 {% else %}
            <div id="unFollowUser" class="follow" data-followsId="{{ followsId }}">
                Unfollow
            </div>
 {% endif %}

I just wanted to change content and functionality to button on click and tried this code with jQuery

$('#followUser').click(function () {
    var userId       = $(this).attr('data-userId'),
        action       = $(this).attr('data-action'),
        currentUser  = $(this).attr('data-currentUserId');

    $.ajax({
        method: 'POST',
        url: "/sci-profile/profile/follow",
        data: {
            userId: currentUser,
            profileUserId: userId,
            action: action
        },
        success: function(response)
        {
            $('#followUser').attr('id', 'unFollowUser')
                            .text('Unfollow')
                            .fadeOut(50)
                            .delay(50)
                            .fadeIn(50);
        }
    });
});

With this code on page source I see different ID and different text on button but when click second time I call first button like it had never changed. Is there some way to refresh memory for that element or I do wrong thing from start?

Stevan Tosic
  • 6,561
  • 10
  • 55
  • 110
  • I had found solution with this question: **http://stackoverflow.com/questions/38686742/reload-js-function-when-load-element-with-ajax** – Stevan Tosic Aug 07 '16 at 20:05

1 Answers1

3

I think your code should like this

{% if followsId == null %}
            <div data-action="follow" class="follow" data-user-id="{{ profileUserData.id }}">
                Follow
            </div>
 {% else %}
            <div data-action="unfollow" class="follow" data-user-id="{{ followsId }}">
                Unfollow
            </div>
 {% endif %}

No need to store current logged in user id, because you can grab it from session :)

Your Ajax Code should be like this

$('.follow').click(function () {
    var _this= $(this);
    var userId = $(this).data('user-id');
    var action =$(this).data('action');

    $.ajax({
        method: 'POST',
        url: "/sci-profile/profile/follow",
        data: {
            profileUserId: userId,
            action: action
        },
        success: function(response)
        {
            if(action=="follow")
            {
                 _this.attr('data-action', 'unfollow').text('Unfollow');
            }
            else
            {
                 _this.attr('data-action', 'follow').text('Follow');
            }
        }
    });
});

Hope you like my answer and up vote it, mark as correct if it is correct :)

Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
  • 2
    +1, and you might want to use `$(document).on("click",".follow",function(event){ var $this = event.target; ..` for dynamically appended (ajax maybe) buttons. – Taha Paksu Jul 25 '16 at 11:24
  • That's not my question :) I'm just thinking forward as if that would be the asker's next problem :) – Taha Paksu Jul 25 '16 at 11:30
  • Rewind it back, this won't work :) you forgot to change the `$(this)` to `_this` after the second line. – Taha Paksu Jul 25 '16 at 11:31