[thelist] Select - forms

Chris Marsh chris at ecleanuk.com
Mon Mar 10 11:15:01 CST 2003


Andrew

> lol I checked the source but alas that pesky javascript looks
> like gobbledeegook.  I'll read the article :)
>
> Is my specific case a bit complicated then!

No, it's very simple. Conventionally, one would submit a form using the
submit button. This is straightforward HTML, and is supported
throughout. However, one may submit a form using javascript instead if
one wishes. The syntax for this varies, but one must always use the
document object model thus:

document.forms[n].submit() (where n is the 0-based index of the form on
the page)
document.forms['formNAME'].submit() (where formNAME is the value of the
name attribute of the form)
document.formNAME.submit() (where formNAME is the value of the name
attribute of the form)

I would recommend the latter, as it is both explicit and elegant (IMHO),
and does not fall over if the order of forms on the page changes.

However, at this stage we know that the code displayed above *will*
submit the form, but we do not know how to tell this code to execute
it's special brand of javascript magic. For this we require an event.
Within the scope of this discussion, an event is something that happens
within an HTML page; that we can detect. There is an onLoad event, so we
could include the following in our page: <body
onLoad='document.formNAME.submit()'>. This would be a pretty pointless
exercise for the majority of people, as it would result in an infinite
loop of form submission. However, we could also include the following:
<a href='javascript:document.formNAME.submit()'>Submit</a>. This is more
relevant, as it allows the submission of a form from a hyperlink. If we
were to include the following: <input type='text'
onFocus='document.formNAME.submit()' /> then the form would be submitted
every time a user clicked on the text field. This (again) is pointless,
and is merely illustrating the point. Hence, if you include the
following:

<select onchange='document.formNAME.submit()'>
<option>Foo</option>
<option>Bar</option>
</select>

Then every time the selection is changed, the form will be submitted.

HTH a little at least...

Regards

Chris Marsh




More information about the thelist mailing list