Shaun Mccran

My digital playground

03
A
U
G
2009

Writing back to a multi select box using javascript

During the building of an online form I discovered that there was the need for a user to select multiple values from a select form field. This is done easily enough using multiple="true". What I then found was that I wanted them to be able to add more options to the select field, preferably without reloading the entire template. These options would then also be 'selected' on being added to the select box. Below is a test script demonstrating this.

Initially I created a standard select form field, and populate it with a simulated query.

view plain print about
1<!--- Create a test query. --->
2<cfset variables.qOptions = QueryNew( "id, name" ) />
3
4<cfset QueryAddRow( variables.qOptions ) />
5<cfset variables.qOptions[ "id" ][ variables.qOptions.RecordCount ] = "1" />
6<cfset variables.qOptions[ "name" ][ variables.qOptions.RecordCount ] = "Value 1" />
7
8<cfset QueryAddRow( variables.qOptions ) />
9<cfset variables.qOptions[ "id" ][ variables.qOptions.RecordCount ] = "2" />
10<cfset variables.qOptions[ "name" ][ variables.qOptions.RecordCount ] = "Value 2" />
11
12<cfset QueryAddRow( variables.qOptions ) />
13<cfset variables.qOptions[ "id" ][ variables.qOptions.RecordCount ] = "3" />
14<cfset variables.qOptions[ "name" ][ variables.qOptions.RecordCount ] = "Value 3" />
15
16<cfset QueryAddRow( variables.qOptions ) />
17<cfset variables.qOptions[ "id" ][ variables.qOptions.RecordCount ] = "4" />
18<cfset variables.qOptions[ "name" ][ variables.qOptions.RecordCount ] = "Value 4" />
19<select name="item" id="item" class="input_text" size="4" multiple="true">
20<cfloop query="variables.qOptions">
21        <option value="#variables.qOptions.id#">#variables.qOptions.name#</option>
22    </cfloop>
23</select>

Next we will add the form button that allows the user to add new values to the select field. The 'Add' button invokes the javascript function AddItem().

view plain print about
1<input type="text" name="newItem" id="newItem" size="30"> <input type="button" value="Add" onclick="AddItem();">

So a user enters their new item into the text box, and it is inserted into the select field drop down, and automatically selected.

view plain print about
1function AddItem(){
2
3                        var newItemValue = document.getElementById("newItem").value;
4                        var list = document.getElementById('item');
5                        var startNewNum = document.getElementById('item').options.length;
6
7                        var newOption = new Option((newItemValue), newItemValue);
8
9                        // do it if there is a value
10                        if(newItemValue.length !== 0) {
11
12                            newOption.selected = true;
13                            list.options.add(newOption);
14
15                            newItem.value = "";
16                            window.alert('You new value has been added');
17
18                        }
19                    }

The javascript above gets the new item value, and inserts it as the last item in the select list. There is also a check to see if there is any text value in the form field. Just to round it off I remove the text field value, and alert the user.

There is probably a shinier way of doing this in something like JQuery, but I'm not that up to speed on that. It would be interesting to see an example though. (hint!).

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Brian Swartzfager's Gravatar There's a jQuery plugin I've used in a few projects that lets you manipulate the options in a select box in a number of ways (including resorting the options after options have been added or removed):

http://www.texotela.co.uk/code/jquery/select/
# Posted By Brian Swartzfager | 03/08/2009 13:16
Back to top