How do i get the form handle

M

mareeus

Hi,

Let's say we have the following html code (a body with some forms):

<html>
<head>
</head>

<body>
<form name="form1" action="#">
<input type="image"
onmouseover="print_form_name()"
alt="Send"
value="1"
name="Send"
src="submit.gif"/>
</form>

<form name="form2" action="#">
<input type="image"
onmouseover="print_form_name()"
alt="Send"
value="2"
name="Send"
src="submit.gif"/>
</form>
</body>
</html>

I'm interested in the code of print_form_name() function. How can i
make it return the form handle, or run an alert with the form name, or
show me a list of controls of the current form (this is what i want).
Thanks in advance.

Regards,
Marius.
 
B

brunascle

Hi,

Let's say we have the following html code (a body with some forms):

<html>
<head>
</head>

<body>
<form name="form1" action="#">
<input type="image"
onmouseover="print_form_name()"
alt="Send"
value="1"
name="Send"
src="submit.gif"/>
</form>

<form name="form2" action="#">
<input type="image"
onmouseover="print_form_name()"
alt="Send"
value="2"
name="Send"
src="submit.gif"/>
</form>
</body>
</html>

I'm interested in the code of print_form_name() function. How can i
make it return the form handle, or run an alert with the form name, or
show me a list of controls of the current form (this is what i want).
Thanks in advance.

Regards,
Marius.

well, here's a kind of fragile way to do it:

alert(this.parentNode.name);

in other words, your HTML would look like <input ...
onmouseover="alert(this.parentNode.name);" ... />

that should work, assuming that the <input> element is the direct
child of the <form> element (like what you have now). if you were to,
say, put a table inside the <form>, and put the <input> inside the
table, it wouldnt work anymore because the code "this.parentNode"
would be getting the wrong element (probably a <td> or something like
that).

you could also try this:

alert(this.form.name);

it looks like that will work in firefox, but i'm not sure about other
browsers. i dont think that way is "proper" DOM, but it would
certainly be easier.
 
L

Lee

no said:
Hi,

Let's say we have the following html code (a body with some forms):

<html>
<head>
</head>

<body>
<form name="form1" action="#">
<input type="image"
onmouseover="print_form_name()"
alt="Send"
value="1"
name="Send"
src="submit.gif"/>
</form>

<form name="form2" action="#">
<input type="image"
onmouseover="print_form_name()"
alt="Send"
value="2"
name="Send"
src="submit.gif"/>
</form>
</body>
</html>

I'm interested in the code of print_form_name() function. How can i
make it return the form handle, or run an alert with the form name, or
show me a list of controls of the current form (this is what i want).

Instead of
onmouseover="print_form_name()"

use onmouseover="print_form_name(this)"

Then your JS for

print_form_name()

becomes

function print_form_name(x) { // x is the passed parameter
theFormName = x.form.name; //string
alert(theFormName);
theForm = document.forms[theFormName]; //the form object

x.form already IS the form object.
In fact, if they set onmouseover="print_form_name(this.form)",
then x will be the form object.


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top