PDA

View Full Version : needed value just flashes, then disappears


CdnGal
10-13-2002, 02:01 AM
HI all...
I have to do a silly little program where you enter a number, select one of three options from a drop down list to convert the number (ie. feet to meters)..then place the newly converted value in a text box. My coding works, and if I use a window.alert, the value appears there..but I need it to appear in a text box. When I run it, the new value does appear in the correct text box..but only for a flash second. What in the world am I doing wrong?

Here's some of the coding. (I've also attached the file if it will help) Thanks for any help!

function convertValue() {
var x=(document.frm1.fromValue.value)* (document.frm1.conversion.value);
(document.frm1.toValue.value=x);
}
</script>
</head>
<body>
<h3>Various Conversions</h3>
<h4>Enter a number to be converted and select one of the three options</h4>
<form name="frm1">
<table>
<tr>
<td><b>From Value</b></td>
<td></td>
<td><b>To Value</b></td>
</tr>
<tr>
<td>
<input type="text" name="fromValue" value="" size="15">
</td>
<td>
<select name="conversion">
<option value="0.4047">Acres to Hectares</option>
<option value=".305">Feet to Meters</option>
<option value="4.54">Imp Gallons to Litres</option>
</select>
</td>
<td><input type="text" name="toValue" value=""size="15"><br>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Convert" onClick=convertValue()>
</td>
</tr>
</table>
</form>

Carl
10-13-2002, 02:21 AM
Add this to your forum tag.

onSubmit="return false;"

Carl

PauletteB
10-13-2002, 02:45 AM
function convertValue() {
var x=(document.frm1.fromValue.value)* (document.frm1.conversion.options[document.frm1.conversion.selectedIndex].value);

(document.frm1.toValue.value)=x;
}


<input type="button" value="Convert" onClick=convertValue()>

CdnGal
10-13-2002, 04:40 AM
Thank you!

Both options worked! But Carl, can I ask you why yours worked? I'm new to Javascript, so I looked up the onSubmit in my book and don't understand why this worked, or why mine didn't in the first place. I'd just like to know for future reference!

Thanks again!

adios
10-13-2002, 05:43 AM
If you use a <input type="submit"> the form will be submitted after the onclick handler is run; before this, the Form.onsubmit handler is also called, and (unconditionally) returning false on it cancels the submission (which caused the 'flash'). Use PauletteB's approach; if you're not submitting, don't use a submit button.

PauletteB
10-13-2002, 06:16 AM
To add to adios's comment

to make the script work with older Netscape, you'd need

[document.frm1.conversion.selectedIndex]

glenngv
10-14-2002, 04:31 AM
Originally posted by PauletteB
To add to adios's comment

to make the script work with older Netscape, you'd need

[document.frm1.conversion.selectedIndex]

not only with old NS but all other browsers other than IE!

that is the correct way of getting the value of a select tag

PauletteB
10-14-2002, 10:45 AM
glenngv

I had known that to be the case till I tried with Netscape 7.0 and the script worked without the [selectedIndex...] hence the qualifier...
Weird...