Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass form field to javascript function

Status
Not open for further replies.

arunjp

Programmer
Jan 10, 2001
29
US
Hi !
I was trying to pass formname and formfield as two arguments to javascript function ononClick event. But in the javascript function, it's not able to recognise the formname and fieldname as form objects. Something like this,

---HTML-----
<INPUT TYPE=&quot;button&quot; NAME=&quot;next&quot; VALUE=&quot;Next&quot; onClick=&quot;signUp4(formname, fieldname);&quot;>
---HTML-----

----Javascript-----
function signUp4(formname, fieldname)
{
alert(document.formname.fieldname.value);
}
---Javascript-----

But the above function throws error.

How to pass the form and form field and access both as form objects, so that I can access the value or length (if it's radio or checkbox).

Thanks
AJP
 
Hi arunjp,

You can try this code:
<html>
<head>
<title>My page</title>
<script language=&quot;javascript&quot;>
function getvalues(refer_form)
{
formname=refer_form.name;
fieldvalue=refer_form.email.value;
alert(&quot;The form name is &quot;+formname+&quot; &quot;+&quot;and the value of the email field is &quot;+fieldvalue);
}
</script>
</head>
<body>
<form name=&quot;myform&quot;>
<input type=&quot;text&quot; name=&quot;email&quot; size=&quot;50&quot;>
<br>
<br>
<input type=&quot;button&quot; name=&quot;mybutton&quot; value=&quot;button1&quot; onclick=&quot;getvalues(this.form)&quot;>
</form>
</body>
</html>


I built a simple form similar to yours.I included a field named &quot;email&quot; as an example.You can include what you wish.
The onclick event handler is applied to the form button.
I pass the form to the function using &quot;this.form&quot; as an argument.
Then,inside the function you can get the form name and the values of all the form elements you might include.
There are many ways of doing this,but I think this is one of the best.I don't know if this is clear enough but I am trying to help.

I hope this was what you are looking for.

Best Regards

alexfusion
 
If you only need the value then just do this:

---HTML-----
<INPUT TYPE=&quot;button&quot; NAME=&quot;next&quot; VALUE=&quot;Next&quot; onClick=&quot;signUp4(document.myFormname.myFieldname.value);&quot;>
---HTML-----

where myFormname is the name of your form and myFieldname is the name of the field you want. You JS function would then be:

----Javascript-----
function signUp4(value)
{
alert(value);
}
---Javascript-----

However, if you need the field and the form, then just do the following:

---HTML-----
<INPUT TYPE=&quot;button&quot; NAME=&quot;next&quot; VALUE=&quot;Next&quot; onClick=&quot;signUp4(document.myFormname, document.myFormname.myFieldname);&quot;>
---HTML-----

----Javascript-----
function signUp4(formname, fieldname)
{
alert(fieldname.value);
}
---Javascript-----


Another alternative is that you pass the actual text names of your field and form which I think you might have been referring to. In this case you would need something like the following:

---HTML-----
<INPUT TYPE=&quot;button&quot; NAME=&quot;next&quot; VALUE=&quot;Next&quot; onClick=&quot;signUp4('myFormname', 'myFieldname');&quot;>
---HTML-----

----Javascript-----
function signUp4(formname, fieldname)
{
alert(eval(&quot;document.&quot; + formname + &quot;.&quot; + fieldname).value);
}
---Javascript-----

Mise Le Meas,

Mighty :)
 
That might be a bit confusing. It assumes that your form code would be something like:

<FORM NAME=&quot;myFormname&quot;.........>
.
.
<INPUT TYPE=&quot;TEXT&quot; NAME=&quot;myFieldName&quot;...>
.
.
</FORM> Mise Le Meas,

Mighty :)
 
If you pass the form NAME and field NAME, then they aren't objects, they're strings. You can pass the OBJECTS using this and this.form. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top