RobB said:
[...]>
Probably a good idea not to disable them - if you want them to submit
If the content of the text input is dependent solely on which checkbox
the user has selected, then only the checkbox has to be submitted -
there is no point in submitting the text input's text (see 3 below).
A common trick is to write the value to a hidden text box that does
get submitted. However, that is redundant too as above.
[...]
Yes, your solution works as requested by the OP, however I think
messing with the user interface like this annoying, especially if the
navigation jumps several inputs.
Given the minimal information provided in the OP's post, there are two
reasons for using a text area in this case:
1. The text input is editable or not depending on the user's selection.
In this case, a text input is used because sometimes you want the user
to enter text and other times control it programmatically.
In this case, your solution could be modified so that the readonly
attribute is set only when certain condition are met. It would be
less annoying to provide a visual que that the text input has changed
to readonly rather than moving the cursor to some other place.
2. To display text that will be submitted to the server. In this case,
the text input is being used like a table cell - as a layout element to
display some text - so it may be best not to use a text input at all.
Surely the label of the checkbox provides all the required information?
Why not display some text in a <span> or similar that is not editable
at all? This would allow the page to be fully functional without
JavaScript.
If the text is controlled by the selection of an option, then only the
checkbox 'sucess' needs to be returned to the server.
3. The OP may ask why not use the displayed text at the server?
Because you don't know that the user isn't spoofing your page, so the
selection of the checkbox *will not* guarantee that the text you want
submitted will be submitted. That is, you must fully validate the form
on the server.
If the user has selected option A, and the associated text is "blah",
then you already know that because A was selected, "blah" is the value.
You don't need "blah" to be returned from the client.
If you do depend on "blah" coming back, you can't guarantee that
because A was selected, "blah" will be returned (i.e. your page has
been spoofed/hacked/whatever).
Some play code follows. I have included a reset function that may not
be needed. Disabling the text input when hidden stops it from being
submitted at all:
<form action="">
<label >Option 1<input type="checkbox" name="chk01"
value="The value of the checkbox"
onclick="
var x = this.form.ta01;
var y = document.getElementById('sp01');
if (this.checked){
x.style.display='none';
x.disabled = true;
y.innerHTML = this.value;
y.style.display = '';
}else{
x.style.display='';
x.disabled = '';
y.style.display = 'none';
}
">
</label><input type="reset" onclick="
document.getElementById('sp01').style.display='none';
this.form.ta01.style.display='';
this.form.ta01.disabled = '';
"><br>
<input name="ta01" type="text" value="" width="200px">
<span id="sp01"></span>
<br>
<input type="submit">
</form>