ajax - How to change link text from jquery function on click event -


i stuck in simple situation.
have link execute ajax call , on success need 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.

it should be:

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

also may keep reference of clicked button/link outside of ajax call , may use inside success callback; this:

$('.vote-up').click(function (e) {     e.preventdefault();     var url = $(this).attr('href'),     btn = $(this); // <-- reference button/link     $.ajax({         url: url,         type: "get",         success: function (html) {             alert("voted up");             btn.text("new link text"); // <-- use referenced jquery object         }     }); }); 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -