View Full Version : Is it possible to make the listbox text always default?

08-30-2004, 05:10 AM
Hi, I have the following listbox which, together with another javascript, able to perform unit conversions.

So the user enters a value in a textbox, and by selecting for e.g. inch, the value will be automatically converted to its meter equivalent.

<select name="L_Units" class="style4" style="width:61px;" onChange="Units_Conversion1()">
<option selected>m</option>
<option value="mm">mm</option>
<option value="inch">inch</option>
<option value="ft">ft</option>

Since meter will always be the default unit, I wonder if this is possible: if the user selects millimeter/inch/feet from the listbox, rather than showing the label mm/inch/ft, the listbox label will automatically goes back to m (meter)...?

08-30-2004, 05:51 AM
Do you mean you want to restrict selection of mm/inch/ft options and only allowing m (meter)? Why not just put the meter option?

08-30-2004, 06:05 AM
Hi glenngv,

Thanks for reply. Actually what I meant is the listbox is needed to tell which unit to convert, therefore I can't allowing only meter (m) option.

When the user enters say, 1000 in the textbox, if he means it to be 1000 meter, then he doesn't have to select any other options in the listbox because the default is already meter (m). But if he means it to be 1000 inch, he will need to select the "inch" option so that the background javascript will automatically convert the 1000 inch to 25.40 meter. And so on...

Because now the user has selected "inch", the listbox shows "inch" as the selected option. Since the conversion has already taken place, it might be confusing to the user as later he might think that is 25.40 inch and not 25.40 meter.

So I thought there could be a way of making the listbox option back to "m" immediately after the user selects "inch" and after the conversion taken place...

08-30-2004, 06:50 AM

Where is your function?

<option selected>m</option>

Since it is selected, just reset the form.


08-30-2004, 06:54 AM
Put this after the conversion in Units_Conversion1() function

document.forms[0].L_Units.selectedIndex = 0;

Or you can optimize your function by passing the this keyword that refers to the current element which is the L_Units combobox.

<select name="L_Units" class="style4" style="width:61px;" onChange="Units_Conversion1(this)">
function Units_Conversion1(objLUnits){
//reset to default selected
objLUnits.selectedIndex = 0;

So all references to the L_Units combobox in the Units_Conversion1() function should be changed to just objLUnits.

08-30-2004, 07:37 AM
Thanks guys! That is exactly what I want :thumbsup: