Forti2ude wrote:
Hello,
I have a simple form...
[snip]
When a user chooses any of the options in the "foo" multiselect, as
long as one of them is value 2, I need to enable the fields "boo" and
"goo".
Anyone have any tips? Can this be done? Thanks!
yup. You need to think whether if the user de-selects "2", should boo
and goo be cleared? I have used onMouseUp for convienience here, but
you should consider carefully what the user would like.
If they select 2, add some text, then select another value, the text box
should be disabled again. If the content isn't cleared, they have to
select 2 again, clear the text, then select their new option. A reset
button fixes this but requires them to actually reset the form, then
select the new value.
You could also use a button to read the value and enable/disable the
text boxes.
<form>
<select name="foo" multiple onMouseUp="
var a = this.form.getElementsByTagName('option');
for (var i=0; i<a.length; ++i) {
if (a[i].selected == true && a[i].value == '2') {
this.form['boo'].removeAttribute('disabled');
this.form['goo'].removeAttribute('disabled');
break;
} else {
// clears the fields, you may not want to do this
this.form['boo'].value = '';
this.form['goo'].value = '';
// and disables them again
this.form['boo'].setAttribute('disabled','disabled');
this.form['goo'].setAttribute('disabled','disabled');
}
}
">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
<input type="text" name="boo" DISABLED>
<input type="text" name="goo" DISABLED>
</form>
However, the *best* option in my book is to only display the boxes if
the user has selected 2. If 2 becomes unselected, hid them again but
don't clear the content. If the user submits the form with some text in
the boxes but 2 not selected (the text boxes will be hidden so the user
may not realise there's something in them), deal with it at the server
by just ignoring the text.
Replace the select tag with the following:
<select name="foo" multiple onMouseUp="
var a = this.form.getElementsByTagName('option');
for (var i=0; i<a.length; ++i) {
if (a[i].selected == true && a[i].value == '2') {
// show them
this.form['boo'].style.visibility='visible';
this.form['goo'].style.visibility='visible';
break;
} else {
// hide them again
this.form['boo'].style.visibility='hidden';
this.form['goo'].style.visibility='hidden';
}
}
">
Note that you should probably hide the text boxes using JS in the first
place, else if JS is disabled and you hide them with styles (as I've
done) your users will never see the text boxes.
Also, you can use display = 'none' and display = '' but this may mess
with the layout of your page.
Fred.