Forms in JavaScript

JavaScript file

So far, we have described a number of different topics, and now it’s time for an article about forms in JavaScript.

Forms, especially in combination with CSS and JavaScript, are providing a strong foundation for development of UI elements to interact with the user.

Forms in JavaScript

Handling forms in JavaScript from scratch.

The form markup and document.form(s):

<form name="test">
  <input name="el1" />

Access to the form object:


Access to the field (element):


// another way

Being inside of a form we can pass it to a function by this.form.

The checkbox input

<form name="myform">
  <input type="checkbox" name="cb" id="cb" />

Example — handling checkbox field type:


// or
document.forms.myform.elements["cb"].checked ? "yes" : "no";

Radio buttons

var f = document.forms.myform;
var btn = f.elements["radio"][0];

We get an access to btn.value and btn.checked.


<form name="myform1" action="index.php" method="get">
    <input type="radio" name="radio1" value="x1" />
    <input type="radio" name="radio1" value="x2" />

The radio1 property of this form points to a list of all the fields in the radio1 group, so the reference will look like this:


For each item in this group can be referenced using square brackets, e.g.:


// or through the item() method:
var f1 = document.forms.myform1;

So if you need to find out if the first field (index = 0) in the radio1 group in myform1 form is marked, you should write:


// or

Value — get and set value.

In general the case is simple: the value of fields can be obtained using .value property, for example:

In a similar way, we can set (dynamically change) the value for the form field.

Example — setting the value of a form field in JavaScript:

document.forms['myform1'].username.value = 'John';

Selection list

<form name="myform1" action="index.php" method="get">
    <select name="list1" multiple>

Following code gives index of selected item, or -1 if nothing selected:

var f = document.forms.myform;
var i = f.elements["list1"].selectedIndex;

We get access to properties: value , text (the text), and options (an array).

Example — another way:

var x_s = document.forms.NameOfForm.myListName.selectedIndex;

References to selection lists can be written in various formats:


In modern browsers there is a shortcut to selected item:

Blazor EditForm Examples.

var f = document.forms.myform;

However, for maximum compatibility it is best to write:


The textarea

To the textarea type field called text1, we refer using the code:


Disabled i readOnly

Objects representing the elements of the form (e.g. “text” input), can be turned off or set to “read only”, so that their modification would be impossible.

Setting disabled = true turns off this field, but also changes its appearance. This can be avoided by using the readOnly attribute.


<input type="text" name="firstname" id="firstname" />
<a href="javascript:turnOffTheField()">Click to turn off</a>
function turnOffTheField() {
    document.myform1.firstname.disabled = true;
    // document.myform1.firstname.readOnly = true;

More about form objects

The total number of forms (in the document) is obtained by reading the property length:

var number_of_forms = document.forms.length;

An access to particular forms:


// or

// or

Properties of the Form object

– elements – an array containing elements of the form,

– length – a number of form elements,

– name – a value of the attribute name (form name),

– acceptCharset – attribute accept-charset, the list of supported code pages,

– action – value of the action parameter, specify where you want to send data from a form,

– enctype – the method of encoding data for the POST method,

– encoding

– method – mode of sending data to the server,

– target – a value for target parameter, e.g. determine the frame to which you want to send data.


– reset() – resets the values ​​of form fields,

– submit() – sends the form.


onreset – when the Reset button is pressed,

onsubmit – when the Submit button is pressed (send form).


That’s all when it comes to the basic processing of the form fields of various type in JavaScript, and the extraction of values.

In the near future we will describe support for forms using JS frameworks and excellent HTML5 forms.