Guest User

Untitled

a guest
Aug 15th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. Getting values from checkbox, add them, and display the result
  2. <div id="pakker">
  3. <input type="checkbox" value="39" />test 1<br/>
  4. <input type="checkbox" value="79" />test 2<br/>
  5. <input type="checkbox" value="29" />test 3<br/>
  6. <input type="checkbox" value="49" />test 4<br/>
  7. ect
  8. </div>
  9.  
  10. var total = 0;
  11. $("#pakker input[type='checkbox']:checked").each(function() {
  12. total += parseInt(this.value, 10);
  13. });
  14. //Do whatever you like with total
  15.  
  16. var result = 0;
  17. $("#pakker input:checked").each(function(){ result += $(this).val(); });
  18.  
  19. alert(result );
  20.  
  21. var sum = $('#pakker :checkbox:checked').map(function() {
  22. return parseInt(this.value, 10);
  23. }).get().reduce(function(prev, current) {
  24. return prev + current;
  25. }, 0);
  26.  
  27. $(document).ready(function(){
  28. $("input[type='checkbox']").click(function(){
  29. var total = 0;
  30. $("#pakker input[type='checkbox']:checked").each(function() {
  31. total += parseInt(this.value, 10);
  32. });
  33. $('#result').html(total);
  34. });
  35. });
  36.  
  37. <span id="result"></span>
  38. <div id="pakker">
  39. <input type="checkbox" value="39" />test 1 - value 39<br/>
  40. <input type="checkbox" value="79" />test 2 - value 79<br/>
  41. <input type="checkbox" value="29" />test 3 - value 29<br/>
  42. <input type="checkbox" value="49" />test 4 - value 49<br/>
  43. ect
  44. </div>
Add Comment
Please, Sign In to add comment