...

View Full Version : Function to populate a selectbox



viktor
03-04-2003, 12:49 PM
Hello! I desperately need a function that populates a selectbox from regular text-links, and is called like this:
<a href="#" onClick="(myselectbox', '14', 'myoption');">blaha</a>

...when clicked, the value "14" and the option "my option" is added to the selectbox "myselectbox".

There must also be
-a possibility to remove options
-a check in the function so that the same value isn't added twice.
-a way to submit every value that has been added to the box (submit it as a commadelimited string I guess)

And is there a way of creating the function so that it works for several boxes? Really thankful if someone can help!

vickers_bits
03-04-2003, 01:08 PM
sorry, but if you so desparately need this functionality, perhaps you might need to part with some of that proverbial fat cash

viktor
03-04-2003, 02:59 PM
OK, anyone else? This script is supposed to move content between selectboxes... but it doesn't work, i get the error "document.choiceForm.BoxTo.length is null or not an object"... Please help!

<html>

<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function moveOver(BoxFrom,BoxTo)
{
var boxLength = document.choiceForm.BoxTo.length;
var selectedItem = document.choiceForm.BoxFrom.selectedIndex;
var selectedText = document.choiceForm.BoxFrom.options[selectedItem].text;
var selectedValue = document.choiceForm.BoxFrom.options[selectedItem].value;
var i;
var isNew = true;
if (boxLength != 0) {
for (i = 0; i < boxLength; i++) {
thisitem = document.choiceForm.BoxTo.options[i].text;
if (thisitem == selectedText) {
isNew = false;
break;
}
}
}
if (isNew) {
newoption = new Option(selectedText, selectedValue, false, false);
document.choiceForm.BoxTo.options[boxLength] = newoption;
}
document.choiceForm.BoxFrom.selectedIndex=-1;
}
function removeMe(BoxFrom,BoxTo) {
var boxLength = document.choiceForm.BoxTo.length;
arrSelected = new Array();
var count = 0;
for (i = 0; i < boxLength; i++) {
if (document.choiceForm.BoxTo.options[i].selected) {
arrSelected[count] = document.choiceForm.BoxTo.options[i].value;
}
count++;
}
var x;
for (i = 0; i < boxLength; i++) {
for (x = 0; x < arrSelected.length; x++) {
if (document.choiceForm.BoxTo.options[i].value == arrSelected[x]) {
document.choiceForm.BoxTo.options[i] = null;
}
}
boxLength = document.choiceForm.BoxTo.length;
}
}
function saveMe(BoxFrom,BoxTo) {
var strValues = "";
var boxLength = document.choiceForm.BoxTo.length;
var count = 0;
if (boxLength != 0) {
for (i = 0; i < boxLength; i++) {
if (count == 0) {
strValues = document.choiceForm.BoxTo.options[i].value;
}
else {
strValues = strValues + "," + document.choiceForm.BoxTo.options[i].value;
}
count++;
}
}
if (strValues.length == 0) {
alert("You have not made any selections");
}
else {
alert("Here are the values you've selected:\r\n" + strValues);
}
}
// End -->
</script>
</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document -->

<BODY>

<center>
<form name="choiceForm">
<table border=0>
<tr>
<td valign="top" width=175>
Move this content...
<br>
<select name="move1a" size=10 onchange="moveOver('move1a','move1b');">
<option value=1>Company News
<option value=2>Industry News
<option value=3>Product Updates
<option value=4>Product Specifications
<option value=5>Order History
<option value=6>Order Status
<option value=7>Contacts
<option value=8>Calendar of Events
<option value=9>Scheduler
<option value=10>Notes
</select>
</td>
<td valign="top">
...over here
<br>
<select multiple name="move1b" style="width:150;" size="10">
</select>
</td>
</tr>
<tr>
<td colspan=2 height=10>
<input type="button" value="Remove" onclick="removeMe(move1a,move1b);">
<input type="button" value="Get Selected Values" onclick="saveMe();">
</td>
</tr>
</table>
</form>
</center>

Vladdy
03-04-2003, 05:16 PM
Originally posted by vickers_bits
sorry, but if you so desparately need this functionality, perhaps you might need to part with some of that proverbial fat cash
:D :D some may call it extorsion :D :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum