Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between this.form and document.forms

Is there a difference between this.form and document.forms (document["forms"]) or, are they similar?

Here is the code I wrote to test the difference.

<form name="myForm" id="myForm">
<input type="text" name="haha" id="myForm" value="laughable" onclick="alert(this.form.haha.value)" />
</form>

alert(document.forms.myForm.haha.value);

They both result in the same thing.

like image 505
W3Geek Avatar asked Jun 14 '12 22:06

W3Geek


1 Answers

this.form will give you the form of the form element. (this is the form element)

The containing form element, if this element is in a form.

document.forms will give you all the forms in the document (if it's supported!)

forms returns a collection (an HTMLCollection ) of the form elements within the current document.

Better use document.getElementById(id)

var form = document.getElementById(formId);
like image 120
gdoron is supporting Monica Avatar answered Sep 20 '22 20:09

gdoron is supporting Monica