Reputation: 107
So if I have a form called myForm and a field called myField, I know I can access that field by using:
document.myForm.myField.value
Is there a way to assign the form itself to a variable like:
var f = document.myForm
so that I can access the fields by just using:
f.myField.value
in order to save myself some typing?
I tried it and it didn't work so I don't know if it's not possible or if I'm just doing something wrong.
Upvotes: 1
Views: 49
Reputation: 332
Yeah of course you can do it! Just add a variable like var act_form = document.forms["myform"]; //make use of it
var action =act_form.action, method = act_form.method;
alert(method);
and in the form contains
<form name="myform" method="post" action="example.com"></form>
Upvotes: 0
Reputation: 944204
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<form id="example">
<input name="field" value="default value">
</form>
<script>
var frm = document.forms.example;
alert(frm.elements.field.value);
</script>
</body>
</html>
Upvotes: 4