DOM - Change event

About

change is an event that is fired when:

  • a control loses the input focus
  • and its value has been modified since gaining focus.

Attribute

You can attach a callback function to the change event with the onchange attribute.

This attribute applies only to the following elements:

Example

Radio

An example with a two radio element that will fire the change event when choosing one or the other.

  • A serie of radio input with the same name (ie choice) and different value
<p>Select a radio to fire the change event</p>
<p><input type="radio" name="choice" id="choice1id" value="choice1" onchange="return handleChange(this);"><label for="choice1id">choice 1</label></p>
<p><input type="radio" name="choice" id="choice2id" value="choice2" onchange="return handleChange(this);"><label for="choice2id">choice 2 </label></p>
  • The callback to handle the change event.
let handleChange = (element) => {
   console.log("The event "+event.type+" was fired and the input radio button chosen has the value "+element.value);
}

Powered by ComboStrap