1

I have an upvote link. When user clicks on it, it hides the link (with upvote image) and shows image without link. It is very basic, see jsfiddle here.

My question is, how can I increase the value in <span class="vote_no">0</span> with + 1 upon click? So if value in that span tag is 23, then it should become 24.

jQuery:

jQuery(".vote_link").click(function() {
    // Hide link
    jQuery(this).andSelf().hide();

    // Show image
    jQuery(this).next(".vote_img_sub").show();
});

HTML:

<div class="vote_cont">
    <span class="vote_no">0</span>
    <a href="#" class="vote_link"><img class="vote_img" src="http://i.myegy.to/images/77e948107f9d.original.gif" width="52" height="30" /></a>
    <img class="vote_img_sub" src="http://i.myegy.to/images/77e948107f9d.original.gif" width="52" height="30" />
</div>
1
  • Are you saying that you also want to remove the link after the user clicks the image? Commented Jan 8, 2015 at 17:03

3 Answers 3

3

You can use:

 $(this).prev().text(parseInt($(this).prev().text(),10)+1);

Working Demo

Sign up to request clarification or add additional context in comments.

3 Comments

Don't forget the radix when using parseInt.
@j08691 What do you mean with radix?
1

I would use .closest() to find the parent and then use .find() to locate .vote_no incase your html ever changed:

$(this).closest(".vote_cont").find(".vote_no").text(parseInt($(this).prev().text(),10)+1);

http://jsfiddle.net/6d5Lgeum/1/

Comments

0

Try this too:

$('.vote_no').text(parseInt($('.vote_no').text())+1)

1 Comment

BTW you forgot to add a class selector . in front

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.