1

I stuck in very simple situation.
I have a link that execute ajax call and on success I need to change clicked link text or color.

foreach (var m in Model.GroupPosts){
...
@Html.ActionLink("Vote up", "Vote", "GroupPost", new { groupPostId = m.GroupPostId }, new{@class="vote-up"})
                @Html.ActionLink("Vote down", "Vote", "GroupPost", new { groupPostId = m.GroupPostId }, new { @class = "vote-down" })
}

$('.vote-up').click(function (e) {
            e.preventDefault();
            var url = $(this).attr('href');
            $.ajax({
                url: url,
                type: "GET",
                success: function (html) {
                    alert("voted up");
                    e.target.text("New link text");
                }
            });
        });

I can't change clicked link text.

2 Answers 2

2

It should be:

$(e.target).text("New link text");

Also you may keep a reference of the clicked button/Link outside of the AJAX call and then you may use it inside your success callback; like this:

$('.vote-up').click(function (e) {
    e.preventDefault();
    var url = $(this).attr('href'),
    btn = $(this); // <-- reference the Button/Link
    $.ajax({
        url: url,
        type: "GET",
        success: function (html) {
            alert("voted up");
            btn.text("New link text"); // <-- Use that referenced jQuery Object
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

And the pure javascript way:

 e.target.innerHTML = "New link text"

e.target is an element, not a jQuery object, so it doesn't have the text() method you are attempting to use. You can either use the element's innerHTML property, or wrap the element in jQuery to give you access to text() as WereWolf demonstrates.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.