Sunday 15 January 2012

javascript - How to find the sum of multiple select's selected options? -



javascript - How to find the sum of multiple select's selected options? -

<select multiple name="item" style="width: 225px;" action="post" id="myselect" onchange="document.getelementbyid('selectedvalue').innerhtml = this.value;"> <option value="10">one <option value="20">two <option value="30">three <option value="40">four <option value="50">five <option value="60">six </select> <p>total: <b><span id="selectedvalue"></span></b></p>

i'm trying total cost displayed if multiple items selected doesn't seem working me. it's simple can't it. help appreciated.

value property stores lastly selected option's value, since have multiple select element, can iterate through selectedoptions collection of htmlselectelement object:

document.getelementbyid('myselect').addeventlistener('change', function() { var total = 0, selected = this.selectedoptions, l = selected.length; (var = 0; < l; i++) { total += parseint(selected[i].value, 10); } // ... });

http://jsfiddle.net/qoft7mre/

javascript

No comments:

Post a Comment