Jump to content

Javascript List/menu Sum


waleshoola

Recommended Posts

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

 

 

 

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="js/jquery.js"></script>

<script type="text/javascript" src="js/jquery-1.4.2.js"></script>

<style>

p { color:red; margin:4px; }

b { color:blue; }

</style>

 

</head>

<body>

<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>

</select>

 

<script>

function displayVals() {

 

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

$("#wal").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

$("#total").html(sum.toFixed(2));

 

 

}

 

$("select").change(displayVals);

displayVals();

 

 

 

 

</script>

 

 

 

 

</body>

</html>

 

 

Can anyone help with this please

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.