Jump to content


Javascript List/menu Sum

javascript sum jquery

  • Please log in to reply
No replies to this topic

#1 waleshoola



  • New Members
  • Pip
  • 1 posts

Posted 11 December 2012 - 11:21 PM

Having issue trying to sum list/menu selected values to show on a textbox

<!DOCTYPE html>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
p { color:red; margin:4px; }
b { color:blue; }

<p><input name="wal" id="wal" type="text"></p>
<p><input type="text" id="total" value="0"></p>

<select size="7" multiple="multiple" id="multiple">
<option value="1" selected="selected">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
<option value="4">Item 4</option>

function displayVals() {

var multipleValues = $("#multiple").val() || [];
multipleValues.join("+ "));

var total =($('#wal').val());
// Update the total

var postid=total.split("+");

var sum = 0;
//iterate through each textboxes and add the values
$("#wal").each(function() {

//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);

//.toFixed() method will roundoff the final sum to 2 decimal places





Can anyone help with this please

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users