javascript - Unable to get div value for voting system -
when click on #upvote
#vote
increases 1 , when #downvote
clicked decreases 1. when vote value "-1" , if upvote clicked vote value becomes "1" , not "0".
<script type="application/javascript"> $(document).ready(function (){ $('#upvote').click(function() { var votevalue = $('#vote').val(); $('#vote').text(votevalue+1); }); $('#downvote').click(function() { var votevalue1 = $('#vote').val(); $('#vote').text(votevalue1-1); }); }); </script> <div id="upvote" style="font-size:22px;">+</div> <div id="vote" style="font-size:22px;">0</div> <div id="downvote" style="font-size:22px;">-</div>
any idea might wrong.
try parseint()
$(document).ready(function (){ $('#upvote').click(function() { var votevalue = $('#vote').text(); $('#vote').text(parseint(votevalue)+1); }); $('#downvote').click(function() { var votevalue1 = $('#vote').text(); $('#vote').text(parseint(votevalue1)-1); }); });
working demo
note: .text()
should use getting value of div instead of val()
Comments
Post a Comment