The OnChange handler is called when any input field changes or any button is clicked.

The Name argument contains the name of the input field or button in lowercase.

Example: Procedure OnChange(Name: String); Begin // If the price or quantity fields are changed, then calculate the amount If Name="price" Or Name="cnt" Then Summa:= RoundMul(Price, Cnt, 2); // If the amount field is changed, then we calculate the price. If Name="summa" Then Price:= If(Cnt=0, 0, RoundDiv(Summa, Cnt, 2)); end;

A similar effect can be obtained when using the OnPriceChange, OnCntChange, OnSummaChange handlers.

Example: Procedure OnPriceChange; Begin Summa:= RoundMul(Price, Cnt, 2); end; On ProcedureCntChange; Begin Summa:= RoundMul(Price, Cnt, 2); end; On ProcedureSummaChange; Begin Price:= If(Cnt=0, 0, RoundDiv(Summa, Cnt, 2)); end;

This handler is convenient to use when organizing modularity, since it collects information about almost all events. For example:

BaseClass: Variant; On ProcedureCreate; Begin BaseDocument.Init(Self); // Create a base class and exchange links with it. Document initialization. end; On ProcedureChange(Name: String); Begin BaseClass.OnChange(Name); // Passing events to the base class end;

Or an example of modularity using a singleton (module property on the editor's Description tab):

On ProcedureCreate; Begin BaseClass.OnCreate(Self); // Document initialization. end; On ProcedureChange(Name: String); Begin BaseClass.OnChange(Self, Name); // Passing events to the base class. end;

Sets the change handler given element form, or fires this event. The method has three use cases:

handler(eventObject)— a function that will be set as a handler. When called, it will receive an event object eventObject .

handler(eventObject)- see above.
eventData— additional data passed to the handler. They must be represented by an object in the format: (fName1:value1, fName2:value2, ...) .

You can remove the installed handler using the unbind() method.

All three options for using the method are analogous to other methods (see above), so all the details of using change() can be found in the description of these methods.

Recall that the change event does not occur directly at the time of the change, but only when the focus of the changed form element is lost.

Example

// set the change event handler to the element with id foo$("#foo" ) .change (function () ( alert ( "Element foo has been changed.") ; } ) ; // call the change event on the foo element$("#foo" ).change() ; // set up another change event handler, this time on the elements // with class block. Pass additional data to the handler$(".block" ) .change (( a: 12 , b: "abc" ) , function (eventObject) ( var externalData = "a=" + eventObject.data .a + ", b=" + eventObject.data .b ; alert ( "An element with class block has been changed."+ "Data passed to this event handler: "+ externalData); ) ) ;

Occurs immediately after the element's value has changed, and the onchange event occurs when the element loses focus. Another difference is that the onchange event has wider browser support and works on elements like , .

Browser Support

Event attribute
Opera

IExplorer

edge
changeYesYesYesYesYesYes

Syntax

onchange="script">

Usage example

An example of displaying text typed in an element on focus change using the onchange event attribute. Note that if we had used the oninput event attribute, the text would have been displayed immediately instead of when the focus changed.

onchange event

Type some text and remove focus from the element:

Type some text and remove focus from the element:

An example of displaying the value of the tag

onchange event

Choose a wish from the list:

Choose a wish from the list:

Wish 1 Wish 2 Wish 3

Consider an example of changing the 2D rotation of an element using CSS property transform , HTML attribute onchange events and JavaScript functions:

2D rotation of an element in CSS

Move the slider to rotate the element:

transform: rotate( 0deg);
Rotate me

_element">

Body ( display: grid; grid-template-areas: "select result"; ) select ( grid-area: select; ) .result ( grid-area: result; )

JavaScript

const selectElement = document.querySelector(".ice-cream"); selectElement.addEventListener("change", (event) => ( const result = document.querySelector(".result"); result.textContent = `You like $(event.target.value)`; ));

result

text input element

For some elements, including , the change event doesn't fire until the control loses focus. Try entering something into the field below, and then click somewhere else to trigger the event.

HTML

JavaScript

const input = document.querySelector("input"); const log = document.getElementById("log"); input.addEventListener("change", updateValue); function updateValue(e) ( log.textContent = e.target.value; )

result

Specifications

Specification Status
HTML Living Standard
The definition of "change" in that specification.
Living Standard

Browser compatibility

The compatibility table on this page is generated from structured data. If you"d like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

Update compatibility data on GitHub

DesktopMobile
ChromeedgeFirefoxInternet ExplorerOperasafariandroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
changeChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full supportYesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full supportYesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support Full support

Different browsers do not always agree whether a change event should be fired for certain types of interaction. For example, keyboard navigation in element represents a control that provides a menu of options"> (see bug 126379). Since Firefox 63 (Quantum), this behavior is consistent between all major browsers, however.

I have an input element and I want to keep checking the length of the content and whenever the length becomes equal to a certain size I want to enable the submit button but I am facing a problem with the javascript onchange event as the event only fires when the input element exits outside the scope, not when the content changes.

Onchange does not fire when the content of the name changes, but only fires when the name goes out of focus.

Can anything be done to make this event work on changing the content? or some other event that i can use for this? I found a workaround using the onkeyup function, but it doesn't work when we select some content from the autocomplete of the browser.

I want something that can work when the field content changes, keyboard or mouse... any ideas?

(function () ( var oldVal; $("#name").on("change textInput input", function () ( var val = this.value; if (val !== oldVal) ( oldVal = val; checkLength( val); ) )); )());

This will catch change , keystrokes, paste , textInput , input (if available). And don't shoot more than necessary.

The user agent must send this event when one or more characters. These characters can come from a variety of sources, such as characters resulting from pressing or releasing a key on a keyboard device, input method editor processing, or from voice command. If the "paste" operation generates a simple sequence of characters, i.e. a text pass without any structure or style information, this type of event must also be generated.

Blockquote>

Fired in controls when user changes value

Blockquote>

You will need to use a combination of onkeyup and onclick (or onmouseup) if you want to catch any opportunity.

Here is another solution I am developing for the same problem. However, I use a lot of input fields, so I keep the old value as a user-defined attribute of the elements themselves: "data-value". Using jQuery so easy to manage.

$(document).delegate(".filterBox", "keyup", ( self: this ), function (e) ( var self = e.data.self; if (e.keyCode == 13) ( e.preventDefault( ); $(this).attr("data-value", $(this).val()); self.filterBy(this, true) ) else if (e.keyCode == 27) ( $(this). val(""); $(this).attr("data-value", ""); self.filterBy(this, true) ) else ( if ($(this).attr("data-value") ! = $(this).val()) ( $(this).attr("data-value", $(this).val()); self.filterBy(this); ) ) ));

here, i used 5-6 input fields, has class filterBox, i make filterBy method only if data value is different from own value.