jason
jason

Reputation: 7164

Applying Jquery to a Smarty variable

I'm trying to make a vote system using jQuery and Smarty. Here is vote variable in Smarty :

<p><span id="votenum">{$q_vote}</span> 

I want to increase the vote by 1 if the question is successfully upvoted, this is what I wrote for the jQuery part to increase the vote :

$("votenum").html($("#votenum").html(parseInt($("#votenum"))+1));

But it doesn't work. Can you tell me how can I fix it? Thanks.

Upvotes: 0

Views: 197

Answers (3)

palaѕн
palaѕн

Reputation: 73906

Try this:

$("#votenum").text( parseInt($("#votenum").text()) + 1 );

Upvotes: 2

adeneo
adeneo

Reputation: 318222

$(function() {
    $("#votenum").text(function(i, txt) {
        return parseInt(txt, 10) + 1;
    });
});

An ID is selected with the # sign, and using an anonymous function makes it trivial to add 1 to the value, and you don't have to look up the element twice, just make sure you're using a radix with the parseInt function.

Upvotes: 1

Carol Skelly
Carol Skelly

Reputation: 362430

You can fix the problem using...

$("votenum").html($("#votenum").html(parseInt($("#votenum").text())+1));

But a better way would be..

var votenum = $("#votenum");
var num = parseInt(votenum.text());
votenum.html(num+1);

This is more efficient as it only selects votenum once.

Upvotes: 1

Related Questions