o7planning

Javascript ChangeEvent Tutorial with Examples

View more Tutorials:

Websites to learn foreign languages for free:
Follow us on our fanpages to receive notifications every time there are new articles. Facebook Twitter

1- ChangeEvent

The  change event fires in the following situations:
  1. Users check/uncheck on <input type="checkbox">.
  2. Users make changes on <input type="radio">.
  3. Users change the value of <input> (file, color, range).
  4. Users change the contents of <input> ( text, number, email, password, tel, time, week, month, date, datetime-local, search, url), then focus on other place.
  5. Users change the contents of <textarea>, then focus on other place.
  6. Users change the option on the <select> element.
ChangeEvent inherit all the properties  and method of interface Event.
Example with the change ​​​​​​​event:
changeevents-elements-example.html

<!DOCTYPE html>
<html>
   <head>
      <title>ChangeEvent Example</title>
      <meta charset="UTF-8">

      <style>.title {font-weight:bold;}</style>

      <script>
          function changeHandler(evt)  {
            alert("Changed!");
          }
      </script>

   </head>
   <body>

      <h2>ChangeEvent example</h2>

      <p class="title">Input (checkbox, radio):</p>
      <input type="checkbox"  onchange ="changeHandler(event)"/>
      <input type="radio" name ="gender" value="M" onchange ="changeHandler(event)"/>
      <input type="radio" name ="gender" value="F" onchange ="changeHandler(event)"/>

      <p class="title">Input (range):</p>
      <input type="range" min ="1" max="10" onchange ="changeHandler(event)"/>

      <p class="title">Input (color):</p>
      <input type="color" onchange ="changeHandler(event)"/>

      <p class="title">Input (file):</p>
      <input type="file" onchange ="changeHandler(event)"/>


      <p class="title">Input (image, hidden, buton, submit, reset):</p>
        - (Not support 'change' event)

      <p class="title">Input (text, number, email, password, tel, time, week, month, date, datetime-local, search, url):</p>
      <input type="text" onchange ="changeHandler(event)"/>

      <p class="title">Textarea element</p>
      <textarea onchange ="changeHandler(event)" rows="3" cols="30"></textarea>

      <p class="title">Select:</p>
      <select onchange ="changeHandler(event)">
         <option value ="en">English</option>
         <option value ="de">German</option>
         <option value ="vi">Vietnamese</option>
      </select>

      <p id="log-div"></p>


   </body>
</html>
 
Example with the change event on the <select> element:
changeevents-select-example.html

<!DOCTYPE html>
<html>
   <head>
      <title>ChangeEvent Example</title>
      <meta charset="UTF-8">

      <script>
          function changeHandler(evt)  {
            alert("Language: " + evt.target.value);
          }
      </script>

   </head>
   <body>

      <h2>ChangeEvent example</h2>

      <p class="title">Select Language:</p>
      <select onchange ="changeHandler(event)">
         <option value ="en">English</option>
         <option value ="de">German</option>
         <option value ="vi">Vietnamese</option>
      </select>


   </body>
</html>

 
Event: change vs input
You may consider using the input event because it's quite similar to the change event: 
  • The input event occurs immediately after the value of an element changes, without requiring the element to lose focus.
  • The input event supports elements with the contenteditable="true" attibutes. While change event supports only the <input>, <texarea>, <select> element.

View more Tutorials: