0

How to get the form name when peress a button inside the form ? document.forms[0].name will do the job only if i have one form. How about for multiple forms ?

FORM:

<form name="aaa">
<input type="button" name="submit" value="Submit Form"  onclick="somefunction();">
</form>

FUNCTION:

<script>
function somefunction()
{
var thename=document.forms[0].name;
alert (thename);
}
</script>

1 Answer 1

2

You can do the below

var thename=document.getElementsByTagName('form')[0].name;

(or)

var thename = document.forms[0].name;

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that solve half of the issue, however in that way i can`t use more than one form in the page becouse the function will return the first form allways!
that should be a different question though ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.