Using jQuery, I would like to extract a number from a <span> and use it to calculate a new value.
<div class="post-100" data-postid="100">
<span class="score">3</span>
</div>
I’ve got the general Idea, but I can’t figure out how to reference the existing span value:
if (response.newvoteid == null)
{
$(".post-" + $(this).data("postid") + " .score").text(
parseInt( **EXISTING VALUE + 1 OR -1**));
}
Update: I am now using the following which fails to calculate the new value, but simply adds a number to the text. For example, <span class="score">3</span> becomes <span class="score">31</span>
var number = $(".post-" + $(this).data("postId") + " .score").text();
$(".post-" + $(this).data("postId") + " .score").text(number + 1);
Just pass a function to
.text()that returns the old value incremented (or decremented):