W3c document.forms [0] .fieldname equivalent

I used

document.forms[0].fieldname.value

to get the values ​​in javascript from the form, but I would like to use the name to refer to the field, not 0, what would be the equivalent these days as it <form name="formname">doesn't match?

+3
source share
5 answers

The easiest way is to add an identifier to the input: <input id="fieldname" />and specify a value from JavaScript as follows:

document.getElementById('fieldname').value

or if you use jQuery

$('#fieldname').val();
+3
source

the collection of forms is standard DOM 1, there is nothing wrong with using it.

document.forms.formId.elements.field
+6
source

name= document.forms ' ?

i.e.: ()

<form name="form1">.insert.your.elements.here.with.name.attribute.set.</form>

JS:

document.forms["form1"]["form_item_name"].value;

, , , pls ...: -)

+2

id getElementById:

var value = document.getElementById('idofelement').value;
+1

(document.forms - , .)

a id document.getElementById.

<input type="text" name="fooName" id="fooId" />
...
document.getElementById("fooId").value;

id, document.getElementsByName, , .

document.getElementsByName("fooName")[0].value;
+1

Source: https://habr.com/ru/post/1744927/


All Articles