Sum of multiple dropdown value selections - guidance would be appreciated
Hi folks,
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:
Code:
<SCRIPT LANGUAGE=javascript>
<!--
var sum = 0;
function OnChange(value)
{
sum = sum + value;
return;
}
//-->
</SCRIPT><br>
<body>
<select name=select1 onchange='OnChange(select1)'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br>
<select name=select2 onchange='OnChange(select2)'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<script type="text/javascript">
document.write(sum);
</script>
</body>
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.
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.
I'm in over my head and appreciate any help. I know some PHP but I think a Javascript solution is what's needed (like the onClick function), and I know only a little JS.
This particular thread is close to what I want to do. I've tried to adapt Tirna's last Javascript but totally failed.
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.
Bookmarks