Author | Topic: HTML ComboBox Code | |
---|---|---|
Bruce Anderson | HTML ComboBox Code on Sat, 24 Jan 2009 14:30:06 -0600 This is a way to create a combobox, a very useful item which does not have a pure representation in html. You are left to cobble one together from component parts and a little innovative coding on your own. I am struck by how similar this situation is to what we faced when XBase++ first came out and there was no GET method, per se. Using a phrase from the late 60's, we had to "roll our own". This example lets the user select the "origin" from an array. However, if the array does not include what the user needs, he can write it in. This all happens in what appears to be a single dialog window, but actually is two separate windows superimposed one on top of the other. If the user picks an item from the list, its value is also saved to the input var "m_ORIGIN", which is important var. If the user types an entry into the dialog, this entry is also appended to the select array should he go back to this method of data entry. By making each <td> cell a <div>, this technique can be used multiple times on a single screen. This code is partly borrowed and partly mine. HTML Code <style type="text/css"> .txtBox1 { position: absolute; top: 2px; left: 80px; width: 50px; z-index: 5; } this sets the "text" input behind the "select" input .dropDown1 { position: absolute; top: 2px; left: 80px; width: 75px; this is 25px wider to accommodate the pull down arrow control border: 0; z-index: 0; } </style> oHtml:put([<tr><td><div style="position:relative;height:100%;">Origin: ]) oHtml:put([<input type="text" name="m_ORIGIN" id="m_ORIGIN" class="txtBox1" onkeypress="return fakeCombo(this,'m_O_SEL',event);" /><br />]) oHtml:put([<select name="m_O_SEL" id="m_O_SEL" class="dropDown1" onChange="comboSelect(this,'m_ORIGIN');" />]) for n := 1 to len( aOrigin ) cId := aOrigin[n] oHtml:put([<option value="] + cId + [" >] + cId + [</option>]) next oHtml:put([</select>]) oHtml:put([</div></td></tr>]) JavaScript Code function fakeCombo(xText,xSelect,eEvent) { var S = document.getElementById(xSelect); var L = S.options.length; var found = false; var myIndex = 0; var keycode; if (navigator.appName == "Microsoft Internet Explorer") {keycode = eEvent.keyCode; } else { keycode = eEvent.which } if (keycode == 13) { for (var i=0; i <= L-1; i++) { if (xText.value == S.options[i].value) {found = true; myIndex = i}; } if (found) { S.options.selectedIndex = myIndex; } else { S.options[S.options.length] = new Option(xText.value,xText.value); S.options.selectedIndex = (S.options.length - 1); } return false; } } function comboSelect(xSelect,xText) { document.getElementById(xText).value = xSelect.options[xSelect.selectedIndex].value;} Bruce Anderson Graphical Database Programs Houston ComboBox.jpg | |
Allen Lee | Re: HTML ComboBox Code on Wed, 28 Jan 2009 12:02:45 -0800 Hi Bruce; I really liked your example. It reminds me of the help that Phil and others used to give. What a great guy you are to share your code with our community. | |
Thomas Braun | Re: HTML ComboBox Code on Thu, 29 Jan 2009 09:14:59 +0100 Bruce Anderson wrote: > This is a way to create a combobox, a very useful item which does not have a > pure representation in html. [...] nice work - on which browsers does this work? regards Thomas | |
Bruce Anderson | Re: HTML ComboBox Code on Thu, 29 Jan 2009 07:30:19 -0600 It does work on IE, Firefox, and Safari. I have not tested it on any others. | |
Thomas Braun | Re: HTML ComboBox Code on Fri, 30 Jan 2009 13:14:44 +0100 Bruce Anderson wrote: > It does work on IE, Firefox, and Safari. I have not tested it on any others. Thanks! Thomas |