1 //Common functions for all dropdowns
2 /*----------------------------------------------
3 The Common function used for all dropdowns are:
4 -----------------------------------------------
5 -- function fnKeyDownHandler(getdropdown, e)
6 -- function fnLeftToRight(getdropdown)
7 -- function fnRightToLeft(getdropdown)
8 -- function fnDelete(getdropdown)
9 -- function FindKeyCode(e)
10 -- function FindKeyChar(e)
11 -- function fnSanityCheck(getdropdown)
13 --------------------------- */
15 function fnKeyDownHandler(getdropdown, e) {
17 fnSanityCheck(getdropdown);
19 // Press [ <- ] and [ -> ] arrow keys on the keyboard to change alignment/flow.
20 // ...go to Start : Press [ <- ] Arrow Key
21 // ...go to End : Press [ -> ] Arrow Key
22 // (this is useful when the edited-text content exceeds the ListBox-fixed-width)
23 // This works best on Internet Explorer, and not on Netscape
25 var vEventKeyCode = FindKeyCode(e);
26 // Press left/right arrow keys
27 if(vEventKeyCode == 37) {
28 fnLeftToRight(getdropdown);
30 if(vEventKeyCode == 39) {
31 fnRightToLeft(getdropdown);
35 if(vEventKeyCode == 46) {
36 fnDelete(getdropdown);
39 // backspace key pressed
40 if(vEventKeyCode == 8 || vEventKeyCode==127) {
41 if(e.which) { //Netscape
42 //e.which = ''; //this property has only a getter.
43 } else { //Internet Explorer
44 //To prevent backspace from activating the -Back- button of the browser
46 if(window.event.keyCode) {
47 window.event.keyCode = '';
52 // Tab key pressed, use code below to reorient to Left-To-Right flow, if needed
53 //if(vEventKeyCode == 9)
55 // fnLeftToRight(getdropdown);
57 } // function fnKeyDownHandler
59 function fnLeftToRight(getdropdown) {
60 getdropdown.style.direction = "ltr";
61 } //function fnLeftToRight
63 function fnRightToLeft(getdropdown) {
64 getdropdown.style.direction = "rtl";
65 } //function fnRightToLeft
67 function fnDelete(getdropdown) {
68 if(getdropdown.options.length != 0) { // if dropdown is not empty
69 if (getdropdown.options.selectedIndex == vEditableOptionIndex_A) { // if option the Editable field
70 getdropdown.options[getdropdown.options.selectedIndex].text = '';
71 //getdropdown.options[getdropdown.options.selectedIndex].value = ''; //Use this line only if want to change the internal value too; else this line is not required.
74 } // function fnDelete
78 Since Internet Explorer and Netscape have different
79 ways of returning the key code, displaying keys
80 browser-independently is a bit harder.
81 However, you can create a script that displays keys
83 The following function will display each key
86 The "FindKey.." function receives the "event" object
87 from the event handler and stores it in the variable "e".
88 It checks whether the "e.which" property exists (for Netscape),
89 and stores it in the "keycode" variable if present.
90 Otherwise, it assumes the browser is Internet Explorer
91 and assigns to keycode the "e.keyCode" property.
94 function FindKeyCode(e) {
96 keycode=e.which; //Netscape
98 keycode=e.keyCode; //Internet Explorer
100 //alert("FindKeyCode"+ keycode);
102 } // function FindKeyCode
104 function FindKeyChar(e) {
105 keycode = FindKeyCode(e);
106 if((keycode==8)||(keycode==127)) {
107 character="backspace"
109 else if((keycode==46)) {
113 character=String.fromCharCode(keycode);
115 //alert("FindKey"+ character);
117 } // function FindKeyChar
119 function fnSanityCheck(getdropdown) {
120 if(vEditableOptionIndex_A>(getdropdown.options.length-1)) {
121 alert("PROGRAMMING ERROR: The value of variable vEditableOptionIndex_... cannot be greater than (length of dropdown - 1)");
126 //Dropdown specific functions, which manipulate dropdown specific global variables
128 /*----------------------------------------------
129 Dropdown specific global variables are:
130 -----------------------------------------------
131 1) vEditableOptionIndex_A --> this needs to be set by Programmer!! See explanation.
132 2) vEditableOptionText_A --> this needs to be set by Programmer!! See explanation.
133 3) vPreviousSelectIndex_A
137 --------------------------- */
139 /*----------------------------------------------
140 The dropdown specific functions
141 (which manipulate dropdown specific global variables)
142 used by all dropdowns are:
143 -----------------------------------------------
144 1) function fnChangeHandler_A(getdropdown)
145 2) function fnKeyPressHandler_A(getdropdown, e)
146 3) function fnKeyUpHandler_A(getdropdown, e)
148 --------------------------- */
150 /*------------------------------------------------
151 IMPORTANT: Global Variable required to be SET by programmer
152 -------------------------- */
154 var vEditableOptionIndex_A = 0;
156 // Give Index of Editable option in the dropdown.
158 // if first option is editable then vEditableOptionIndex_A = 0;
159 // if second option is editable then vEditableOptionIndex_A = 1;
160 // if third option is editable then vEditableOptionIndex_A = 2;
161 // if last option is editable then vEditableOptionIndex_A = (length of dropdown - 1).
162 // Note: the value of vEditableOptionIndex_A cannot be greater than (length of dropdown - 1)
164 var vEditableOptionText_A = "Custom";
166 // Give the default text of the Editable option in the dropdown.
168 // if the editable option is <option ...>--?--</option>,
169 // then set vEditableOptionText_A = "--?--";
171 /*------------------------------------------------
172 Global Variables required for
173 fnChangeHandler_A(), fnKeyPressHandler_A() and fnKeyUpHandler_A()
174 for Editable Dropdowns
175 -------------------------- */
177 var vPreviousSelectIndex_A = 0;
178 // Contains the Previously Selected Index, set to 0 by default
180 var vSelectIndex_A = 0;
181 // Contains the Currently Selected Index, set to 0 by default
183 var vSelectChange_A = 'MANUAL_CLICK';
184 // Indicates whether Change in dropdown selected option
185 // was due to a Manual Click
186 // or due to System properties of dropdown.
188 // vSelectChange_A = 'MANUAL_CLICK' indicates that
189 // the jump to a non-editable option in the dropdown was due
190 // to a Manual click (i.e.,changed on purpose by user).
192 // vSelectChange_A = 'AUTO_SYSTEM' indicates that
193 // the jump to a non-editable option was due to System properties of dropdown
194 // (i.e.,user did not change the option in the dropdown;
195 // instead an automatic jump happened due to inbuilt
196 // dropdown properties of browser on typing of a character )
198 /*------------------------------------------------
199 Functions required for Editable Dropdowns
200 -------------------------- */
202 function fnChangeHandler_A(getdropdown, e) {
203 fnSanityCheck(getdropdown);
205 vPreviousSelectIndex_A = vSelectIndex_A;
206 // Contains the Previously Selected Index
208 vSelectIndex_A = getdropdown.options.selectedIndex;
209 // Contains the Currently Selected Index
211 if ((vPreviousSelectIndex_A == (vEditableOptionIndex_A)) && (vSelectIndex_A != (vEditableOptionIndex_A))&&(vSelectChange_A != 'MANUAL_CLICK')) { // To Set value of Index variables -
212 getdropdown[(vEditableOptionIndex_A)].selected=true;
213 vPreviousSelectIndex_A = vSelectIndex_A;
214 vSelectIndex_A = getdropdown.options.selectedIndex;
215 vSelectChange_A = 'MANUAL_CLICK';
216 // Indicates that the Change in dropdown selected
217 // option was due to a Manual Click
219 } // function fnChangeHandler_A
221 function fnKeyPressHandler_A(getdropdown, e) {
222 fnSanityCheck(getdropdown);
224 keycode = FindKeyCode(e);
225 keychar = FindKeyChar(e);
227 // Check for allowable Characters
228 // The various characters allowable for entry into Editable option..
229 // may be customized by minor modifications in the code (if condition below)
230 // (you need to know the keycode/ASCII value of the character to be allowed/disallowed.
233 if ((keycode>47 && keycode<59)||(keycode>62 && keycode<127) ||(keycode==32)) {
234 var vAllowableCharacter = "yes";
237 var vAllowableCharacter = "no";
240 //alert(window); alert(window.event);
242 if(getdropdown.options.length != 0) {
243 // if dropdown is not empty
244 if (getdropdown.options.selectedIndex == (vEditableOptionIndex_A)) {
245 // if selected option the Editable option of the dropdown
247 var vEditString = getdropdown[vEditableOptionIndex_A].text;
249 // make Editable option Null if it is being edited for the first time
250 if((vAllowableCharacter == "yes")||(keychar=="backspace")) {
251 if (vEditString == vEditableOptionText_A)
254 if (keychar=="backspace") {
255 // To handle backspace -
256 vEditString = vEditString.substring(0,vEditString.length-1);
257 // Decrease length of string by one from right
259 vSelectChange_A = 'MANUAL_CLICK';
260 // Indicates that the Change in dropdown selected
261 // option was due to a Manual Click
264 //alert("EditString2:"+vEditString);
266 if (vAllowableCharacter == "yes") {
267 // To handle addition of a character -
268 vEditString+=String.fromCharCode(keycode);
269 // Concatenate Enter character to Editable string
271 // The following portion handles the "automatic Jump" bug
272 // The "automatic Jump" bug (Description):
273 // If a alphabet is entered (while editing)
274 // ...which is contained as a first character in one of the read-only options
275 // ..the focus automatically "jumps" to the read-only option
276 // (-- this is a common property of normal dropdowns
277 // ..but..is undesirable while editing).
280 var vEnteredChar = String.fromCharCode(keycode);
281 var vUpperCaseEnteredChar = vEnteredChar;
282 var vLowerCaseEnteredChar = vEnteredChar;
285 if(((keycode)>=97)&&((keycode)<=122))
286 // if vEnteredChar lowercase
287 vUpperCaseEnteredChar = String.fromCharCode(keycode - 32);
291 if(((keycode)>=65)&&((keycode)<=90))
292 // if vEnteredChar is UpperCase
293 vLowerCaseEnteredChar = String.fromCharCode(keycode + 32);
296 if(e.which) { //For Netscape
297 // Compare the typed character (into the editable option)
298 // with the first character of all the other
299 // options (non-editable).
301 // To note if the jump to the non-editable option was due
302 // to a Manual click (i.e.,changed on purpose by user)
303 // or due to System properties of dropdown
304 // (i.e.,user did not change the option in the dropdown;
305 // instead an automatic jump happened due to inbuilt
306 // dropdown properties of browser on typing of a character )
308 for (i=0;i<=(getdropdown.options.length-1);i++)
310 if(i!=vEditableOptionIndex_A)
312 var vReadOnlyString = getdropdown[i].text;
313 var vFirstChar = vReadOnlyString.substring(0,1);
314 if((vFirstChar == vUpperCaseEnteredChar)||(vFirstChar == vLowerCaseEnteredChar))
316 vSelectChange_A = 'AUTO_SYSTEM';
317 // Indicates that the Change in dropdown selected
318 // option was due to System properties of dropdown
323 vSelectChange_A = 'MANUAL_CLICK';
324 // Indicates that the Change in dropdown selected
325 // option was due to a Manual Click
332 // Set the new edited string into the Editable option
333 getdropdown.options[vEditableOptionIndex_A].text = vEditString;
334 //getdropdown.options[vEditableOptionIndex_A].value = vEditString; //Use this line only if want to change the internal value too; else this line is not required.
342 function fnKeyUpHandler_A(getdropdown, e) {
343 fnSanityCheck(getdropdown);
345 if(e.which) { // Netscape
346 if(vSelectChange_A == 'AUTO_SYSTEM') {
347 // if editable dropdown option jumped while editing
348 // (due to typing of a character which is the first character of some other option)
349 // then go back to the editable option.
350 getdropdown[(vEditableOptionIndex_A)].selected=true;
353 var vEventKeyCode = FindKeyCode(e);
354 // if [ <- ] or [ -> ] arrow keys are pressed, select the editable option
355 if((vEventKeyCode == 37)||(vEventKeyCode == 39)) {
356 getdropdown[vEditableOptionIndex_A].selected=true;
361 /*-------------------------------------------------------------------------------------------- */
363 <!--end of script for dropdown lstDropDown_A -->