PROWARE technologies
PROWARE technologies

JavaScript Tutorial

User Interface Events

User interface events are those events that are not related to user actions.

The contextmenu Event

A context menu is the one that pops up when someone right clicks an object. The event is oncontextmenu.

Scripting with Forms

Originally, JavaScript was used for processing forms client-side thereby freeing up server resources.

Form Basics

Web forms are HTML <form> elements. The <form> element has all of the same default properties as other HTML elements plus some special extra ones. These are:

All forms on the page can be found in the document.forms collection. Each form can be accessed in this collection by numeric index or by name attribute. Example:

var firstForm = document.forms[0];
var form1 = document.forms["form1"]

Fields of the Form

The form elements can be accessed through the elements property of a form object (<form>). Elements include <input>, <textarea>, <button>, <select> and <fieldset>. Consider the following:

var form = document.getElementById("someForm");
var field = form.elements[0];

//change the value
field.value = "Another value";

//check the value of form
alert(field.form === form); //true

//set focus to the field

//disable the field
field.disabled = true;

Every form field has a focus() and blur() methods. focus() is used to set the focus on a certain field. To remove focus from a field invoke blur().

Selecting Text in Text Boxes

To select all the text in a <input type="text"> or <textarea> element, invoke the select() method.

When text is selected by the user, a onselect event is fired.

To get the selected text use the selectionStart and selectionEnd

function getSelectedText(textbox){
return textbox.value.substring(textbox.selectionStart, textbox.selectionEnd);

Use the setSelectionRange() method to set the selected text programmatically.

textbox.value = "just a test!"
//select all the text
textbox.setSelectionRange(0, textbox.value.length);

Dealing with the Clipboard

These six events are related to the clipboard:

<< < [Page 18 of 22] > >>