Link to home
Start Free TrialLog in
Avatar of dgrafx
dgrafxFlag for United States of America

asked on

get form name

how can I get form name or actually the form object would be fine - either one - from a link?
for example : <a href="" onclick="alert(the name of the form this link resides in); return false;">Click</a>

thanks
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Using : document.forms[0]
<a href="#" onclick="alert(document.forms[0].name); return false;">Click</a>

Open in new window


To get the first one (or the only one) : document.getElementsByTagName("form")[0]
<a href="#" onclick="alert(document.getElementsByTagName('form')[0].name); return false;">Click</a>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Proculopsis
Proculopsis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dgrafx

ASKER

closest ??? !!!
never heard of it - if it works - awesome!!!
not doubting you - I just can't test it till tomorrow morning ...

thanks - will be in touch ...
Avatar of dgrafx

ASKER

you the man ...
Avatar of dgrafx

ASKER

one final question on this post if youd be so kind ...
You showed me how to get form name or id - which is what I need some of the time.
Other times I need the form object - ya know like if you had a form named myForm then what I mean is document.myForm will give you the parent form object.

how can i get that?
thanks ...
Just remove the .attr()

var formName = $(this).closest("form").attr("name");
var formObject = $(this).closest("form");
Avatar of dgrafx

ASKER

hey thanks buddy!
I should have opened a new question - sorry ...
We'll get ya next time!
No need. Thanks anyway :)