• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 12. Working with Forms, Buttons,... > Passing Forms to Functions

Passing Forms to Functions

Although you can access data in a form if you know the form’s name (like this: document.form1.button1.value), what if you have multiple forms in a web page you want to work with? How do you know what form a control called your script from? To let your scripts know which form a control is in when an event occurs in that control, you can pass the control’s form to the event handler using the control’s form property like this: this.form. And if you’re using an event of the form itself, such as onsubmit, you can just use the form’s this property to refer to itself.

Here’s an example. In this case, I have two forms in a web page, form1 and form2, to let the user enter her name and age. I connect both forms’ onsubmit event to the same function, checkData, and I pass the form’s this property to that function like this:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint