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

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -