Javascript Display label for an unhidden text box

  MS35 20:25 03 Jun 2008

I'm just trying to get to grips with a simple example in javascript to unhide a text box depending on a selection from a drop down.

In the following example if you select "yes" a new input box appears, but i'd like it to be prefixed by the statement "Please enter your meter reading here"

And secondly how could I change the No/Yes dropdown to a checkbox with the same result?


meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
title>Untitled Document /title>

script type="text/javascript">

function toggleMeter(chosen) {
if (chosen == 'Yes') { = 'visible';
} else { = 'hidden';
document.Electric.Meter.value = ';


form id="Electric" name="Electric">
Do you have a Water Meter?
select name="values" size="1"
onchange="toggleMeter( document.Electric.values.options[ document.Electric.values.selectedIndex ].value );">
option value="No" selected="selected">No /option>
option value="Yes">Yes /option>

input type="text" name="Meter" size="25" maxlength="20" style="visibility:hidden" />


  MS35 19:00 04 Jun 2008

But would still like to know who I can hide the label for the input box until the checkbox is ticked.

Any thoughts?

<script type="text/javascript">

function changeIt() {
if(document.Water.chk.checked){"visible"; //set to visible
}else{"hidden"; //set to hidden

<form name="Water">
<p>Does the property have a water meter?
<input name="chk" type="checkbox" onClick="changeIt()" value="">
Please enter Meter reading here: <input type="text" name="MeterReading" style="visibility: hidden">

  Kemistri 02:05 05 Jun 2008

If you want to use a function more than once, there's nothing stopping you. How you do that is up to you, but the least code heavy option tends to involve classes. Just like CSS.

Read up some more about JS before you worry too much about trying to do things like this (of all things, a form must rank as one of the most important things in which to avoid bugs). And keep in mind the accessibility issues with JS.

This thread is now locked and can not be replied to.

Elsewhere on IDG sites

WPA2 hack: How secure is your Wi-Fi?

Microsoft Surface Book 2 hands-on review – bigger and 5x faster

Best kids apps for iPhone & iPad

Que faire si son iPhone ou iPad est tombé dans de l'eau ?