onchange

HTML (DHTML)

onchange Event

Internet Development Index

Fires when the contents of the object or selection have changed.

Syntax

Inline HTML<ELEMENT onchange = "handler" ... > All platforms
Event propertyobject.onchange = handlerJScript only
object.onchange = GetRef("handler")Visual Basic Scripting Edition (VBScript) 5.0 or later only
Named script <SCRIPT FOR = object EVENT = onchange> Internet Explorer only

Event Information

BubblesNo
CancelsYes
To invoke
  • Choose a different option in a select object using mouse or keyboard navigation.
  • Alter text in the text area and then navigate out of the object.
Default action Changed text selection is committed.

Event Object Properties

Although event handlers in the DHTML Object Model do not receive parameters directly, a handler can query an event object for data.

Available Properties

altKey Sets or retrieves a value that indicates the state of the ALT key.
altLeft Sets or retrieves a value that indicates the state of the left ALT key.
clientX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
clientY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
ctrlLeft Sets or retrieves the state of the left CTRL key.
offsetX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the object firing the event.
offsetY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the object firing the event.
returnValue Sets or retrieves the return value from the event.
screenX Retrieves the x-coordinate of the mouse pointer's position relative to the user's screen.
screenY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the user's screen.
shiftLeft Retrieves the state of the left SHIFT key.
srcElement Sets or retrieves the object that fired the event.
type Sets or retrieves the event name from the event object.
x Sets or retrieves the x-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element.
y Sets or retrieves the y-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element.

Remarks

This event is fired when the contents are committed and not while the value is changing. For example, on a text box, this event is not fired while the user is typing, but rather when the user commits the change by leaving the text box that has focus. In addition, this event is executed before the code specified by onblur when the control is also losing the focus.

The onchange event does not fire when the selected option of the select object is changed programatically.

Example

This example uses the onchange event to retrieve the selected option of a select object.

<BODY>
<FORM>
<P>Select a different option in the
drop-down list box to trigger the onchange event.
<SELECT NAME=selTest
onchange="alert('Index: ' + this.selectedIndex
+ '\nValue: ' + this.options[this.selectedIndex].value)">
<OPTION VALUE="Books">Books
<OPTION VALUE="Clothing">Clothing
<OPTION VALUE="Housewares">Housewares
</SELECT>
</FORM>
</BODY>
This feature requires Microsoft® Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

This event is defined in HTML 4.0 Non-Microsoft link.

Applies To

INPUT type=text, SELECT, TEXTAREA

See Also

onkeypress