Showing posts with label html select. Show all posts
Showing posts with label html select. Show all posts

Wednesday, February 11, 2009

Javascript HTML Select Again

Just a short follow up to yesterday's post with set value and get value functions that will work with both single and multiple select HTML elements. The code expects that you will correctly pass a single value / array value when calling against the select element:



Probably only the set part of this code is of interest to you if you are using Prototype as prototype will give you the correct result with Form.element.getValue or $F.

Tuesday, February 10, 2009

Javascript HTML Select - Multiple Select

Simple Javascript code to set multiple selections in an HTML Select element - The first step is that the select must be set up to allow multiple selections, that is it must have the attribute multiple="multiple". Second, we need some supporting code to find the position of a value in an array - if you are using Prototype or limiting your usage to newer browsers you may not have to roll your own here as indexOf will be a method of the Array object. Otherwise you are going to need this:



Now the code to set the selections. You pass in the select element, an array of selections, and an optional boolean. The boolean defaults to false and indicates whether or not to invert the selection:



Now for a little demo: