[Javascript] Accessing form name

Roger Roelofs rer at datacompusa.com
Mon Nov 21 07:20:07 CST 2005


Shalk,

On Nov 21, 2005, at 7:59 AM, Schalk wrote:

>> On 21/11/05, Schalk <schalk at volume4.com> wrote:
> But here is my problem:
> I want to create a simple generic reset function for forms within  
> Joomla. Now, I do not know beforehand what the form name or id is  
> and therefore wanted to determine this with JavaScript. Here is  
> what I have so far:
>
> function resetForm() {
>   var theForm = document.getElementsByTagName('form');
>   var formName = theForm.name;
>   alert(formName);
>
>   document.formName.reset();
> }
>
> This does get the form but name is returned as undefined. Where am  
> I going wrong or is there a simpler/better way to do this? Thank you.

getElementsByTagName returns an array of form objects.  So, if you  
know for a fact that there will only be one form on the page you  
could write it like this.

function resetForm() {
   var theForm = document.getElementsByTagName('form')[0];
   var formName = theForm.name;
   alert(formName);

   document.formName.reset();
}

However, I don't recommend this approach because invariably someone  
else changes the html and the javascript 'breaks'.  If you could give  
us a little more info about what you are trying to accomplish and  
what triggers the need for the reset we could provide better advice.

Roger
-------------------------------------------------------
Roger Roelofs                          web   www.datacompusa.com
Datacomp Appraisal Services   Email rer at datacompusa.com
3215 Eaglecrest Drive, NE
Grand Rapids, MI  49525-4593





More information about the Javascript mailing list