I'm trying to create a Stackoverflow like voting system, and I've run into a slight problem.
I have the following HTML that has jQuery onClick events wired to it:
<div id="c_<%=Html.Encode(Model.C.cID) %>" class="votes">
<img src="../../Content/gfx/Up.png" class="up" alt="" />
<span class="votecount"><%= Html.Encode(Model.C.VoteCount)%></span>
<img src="../../Content/gfx/Down.png" class="down" alt="" />
</div>
The jQuery onClick looks like this:
$(".up").click(function() {
var id = $(this).parent().attr("id").split("_");
if (id[0] == "c") {
//C Vote
//id[1] contains the id number.
$.post("/Vote/CUp", { id: id[1] }, function(data) {
$(this).parent().children(".votecount").html(data.voteCount);
},
"json"
);
} else {
//R Vote
$.post("/Vote/RUp", { id: id[1] }, function(data) {
$(this).parent().children(".votecount").html(data.voteCount);
},
"json"
);
};
});
My problem lies in trying to update the vote count. I just can't work out how to update the votecount span with the values returned in the JSON object.
The JSON object is returning data, I've verified this using alert(data)
Help is much appreciated.
In the scope of the $.post() callback, this will give you a reference to the XMLHttpRequest object, not the element that raised the earlier click event.
You can assign this to a variable in the scope of the click handler and it will still be available in $.post()'s callback. Something like this:
$(".up").click(function() {
var id = $(this).parent().attr("id").split("_");
// Due to the awesome magic of closures, this will be available
// in the callback. "el" is a weak name for the variable, you
// should rename it.
var el = this;
if (id[0] == "c") {
//C Vote
//id[1] contains the id number.
$.post("/Vote/CUp", { id: id[1] }, function(data) {
// siblings() is an easier way to select that.
$(el).siblings(".votecount").html(data.voteCount);
}, "json");
} else {
//R Vote
$.post("/Vote/RUp", { id: id[1] }, function(data) {
$(el).siblings(".votecount").html(data.voteCount);
}, "json");
};
});