I need some help/advice on constructing a simple JavaScript utility.
Let me preface by saying I've Googled quite a bit today for JavaScript resources, and have found lots of tutorials and repositories of code samples, but nothing that seems to explain the combinations of features I need. I've worked before on modifying/expanding pre-existing code and know my around about the script, but can't code from scratch myself. If some whiz wants to whip something up for me, my gratitude would be very real, but a referral to a reference resource or something would be fine as well.
This is all for personal, non-commercial purposes, BTW. I'll explain what I need for reference:
I'd like to have a series of checkboxes as well as a TEXTAREA text box. Each checkbox will represent a group of values, and when the box is checked, that group's values will appear in the text box along with the values of any other checked group, preferably in alphabetical (and/or numerical) order
For example, the groups could be FRUIT, MEAT and BAKED GOODS and the values for each could be:
FRUIT= "apples, oranges, bananas"
MEAT= "chicken, beef, pork"
BAKED GOODS="cookies, cakes, muffins"
So if a user checked the boxes next to FRUIT and BAKED GOODS, the text box would display "apples, bananas, cakes, cookies, muffins, oranges"
Ideally, each GROUP can also have a price associated which could be totalled in a seperate text box.
It doesn't matter if the values appear in the TEXTAREA immediately upon the checkboxes being checked or after a "submit" input of some kind.
Now, the real icing on the cake would be if a page could display 2 sets of the above form elements, along with an additional TEXTAREA that would contain the difference between the values listed in the original TEXTAREAs. For example, if FRUITS and MEATS were selected in form 1 and MEATS and BAKED GOODS were selected in form 2, the additional TEXTAREA would display "beef, chicken, pork (the shared values), -apples, -bananas, -oranges (the values selected in form 1 but not form 2), +cakes, +cookies, +muffins (the values selected in form 2 but not form 1)"
I've turned to AskMeFi knowing that someone out there could simply code this for me, and I'm in no position to refuse that kind of help - but I'm happy to spend sometime figuring it out if you can offer a some hints or a good tutorial to check out.
posted by chudmonkey to computers & internet (15 comments total)
You almost certainly want to use an array of one array per checkbox, with the values in the nested array. Then you add an onclick function to the textboxes that calls a function that looks up the array element corresponding to the checkbox, and iterates through the values, adding them to a string, and and then settign the textarea.value to the string.
I don't think jaavscript does set operations, do for your icing, the hint is, sort the two sets first, then iterate over both to find intersections.
"This is all for personal, non-commercial purposes, BTW. "
Huh. So what exactly is this for?
posted by orthogonality at 12:28 PM on February 16, 2006