Sum of multiple dropdown value selections - guidance would be appreciated
Not a seasonsed JS user; I would like to have a number of dropdown forms on a page and have JS sum the total value up as the user makes their way down the page and selects values. I've googled the daylights out of this and come up with a proof of concept below - if I could get it to work right, I would apply to a larger page. It will live on a Sharepoint server so I can't just use PHP to handle the summing. Any help on my shortcomings would be greatly appreciated:
var sum = 0;
sum = sum + value;
<select name=select1 onchange='OnChange(select1)'>
<select name=select2 onchange='OnChange(select2)'>
One follow-up question; I notice that if I change a dropdown's value more than once, the function just keeps adding it up and I end up with a score greater than the possible total for the whole page. Is there a reasonable way of handling an event such as this?
ok, that's a different requirement from your first post.
The easiest way now would be to remove your onchange and add a button at the bottom. When you click the button, it gets the current selected value from each select list and sums them.
If you get stuck, post the code you have so far.
Ok, I'm stuck....I'm not confident on what I'm passing into the function - the object model reference stuff I've read isn't sinking in and I'm not sure I'm addressing form elements correctly. The other thing that is inelegant is my attempt at adding - if I add a bunch more dropdowns, a loop with an array would be better but I can't work out the syntax. Thanks for any help - I appreciate it.
My form sends data to a third party site, which limits the number and types of fields I can use for form submission. So the processing has to be client-side. The listbox could have 50 to 100 options that regularly change. I want to concatenate the selected values for the external service. That way I only have one field on the service where I can search on "dog" and find all records with that value regardless of whether it was the only selection option or one of several.
What I need in particular -- I'm open to any solution:
1. A multidimensional array of listbox option values that represent the HTML text (must be defined) and option values (optional, if not provided the value is the same as the text) for each listbox.
2. A form with listbox where the user can select multiple options.
3. When the form is submitted:
A. The selected items are concatenated with a space, like "cat dog pig"
B. The concatenated string is sent as the value for the listbox field.