WikiWealth

06 Aug 2011 03:13 | firefox forms javascript

I recently ran into a problem: some JavaScript code that was working on other browsers (Google Chrome & Internet Explorer) consistently failed when I attempted to use Mozilla Firefox.

In fact, Firefox did absolutely nothing. A loading dialog was meant to appear while things were being processed in the background, and then it was meant to redirect to a new URL. Firefox just decided to refresh the current page instead. Not very helpful!

Looking at the Error Console located under the "Tools" menu, I soon realised that the browser couldn't find my form. For some reason, every other browser saw it, but Firefox thought that it didn't exist. I was stumped.

After spending time searching the Internet for answers, I finally stumbled upon the one I was looking for… I made the necessary changes, and Firefox began working!

What was the problem?

I was accessing a field in the HTML form using code like this:

alert( "Field value: " + myForm.FieldName.value );

But thanks to Bruce Barker, I solved it with something like this:

document.myForm.FieldName.value

The only change that was necessary was to include the 'document' namespace when referring to the form. Both Google Chrome and Internet Explorer didn't need that, and intelligently guessed which form I was referring to.

According to what Bruce posted on the forum that I linked to above, and for future reference:

Accessing a form using Javascript
Any browser that supports JavaScript document.myForm
document.forms['myForm']
document.forms[0]
Most modern browsers document.getElementsByName('myForm')[0]
document.getElementById('myForm')
Google Chrome & Internet Explorer only myForm
document.all('myForm')

Notes:

  • If you see a number instead of the form name, that refers to the index number of the form. If you only have one form on the page it will always be at index 0.
  • When using 'getElementById' you must give the form an ID as well.

P.S. This post was created with STE (a Wikidot Editor).
Image: Copyright © Mozilla Foundation and Mozilla Corporation


Submit Feedback
Please rate this page based on the quality and helpfulness of the content to you.

Average 0% rating from 0 votes.

You may also be interested in…


OLD COMMENT SYSTEM (4 comments)

Add a New Comment
or Sign in as Wikidot user
(will not be published)
- +

Add a new comment