Faqts : Computers : Programming : Languages : JavaScript : Forms

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

14 of 20 people (70%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How can I select certain form elements for submission?

Mar 5th, 2001 04:03
Martin Honnen,


HTML 4 allows to disable FORM elements so that they don't take input and 
are not submitted. You simply add
  <INPUT DISABLED
or
  <TEXTAREA DISABLED
and then you can toggle that value with JavaScript
  document.formName.fieldName.disabled = false;
respectively
  document.formName.fieldName.disabled = true;
This is supported by NN6 and IE4+. Note that Opera 5 seems to support 
the HTML attribute DISABLED but doesn't allow you to change the value 
with JavaScript.
Here is a complete example for NN6 and IE4+ which shows a random number 
of lines with a checkbox and input type="text" fields which are only 
submitted when the checkbox on the line is checked:
<HTML>
<HEAD>
<SCRIPT>
function enable (form, line) {
  var i = 0;
  var input;
  while ((input = form['r' + line + 'c' + i++])) 
    input.disabled = false;
}
function disabled (form, line) {
  var i = 0;
  var input;
  while ((input = form['r' + line + 'c' + i++])) 
    input.disabled = true;
} 
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="formName"
      ACTION="whatever"
>
<SCRIPT>
for (var i = 0; i < Math.floor(Math.random() * 10) + 1; i++) {
  var html = '';
  html += '<INPUT TYPE="checkbox" NAME="cb' + i + 
    '" ONCLICK="if (this.checked) enable(this.form, ' + i + 
    '); else disable(this.form, ' + i  + ');">';
  for (var c = 0; c < Math.floor(Math.random() * 5) + 1; c++)
    html += '<INPUT TYPE="text" NAME="r' + i + 'c' + c + '" DISABLED>';
  html += '<BR>';
  document.write(html);
}
</SCRIPT>
<INPUT TYPE="submit">
</FORM>
</BODY>
</HTML>