Table of contents
1.
Introduction
2.
Syntax of Onchange Event in JavaScript
3.
How does Onchange Event in JavaScript Work?
4.
Examples
4.1.
Example 1: Text Input
4.2.
Example 2: Select Dropdown
4.3.
Example 3: Checkbox
5.
Browser Compatibility
6.
Frequently Asked Questions
6.1.
Can the onchange event be used with other form elements besides input, select, and textarea?
6.2.
Is it possible to detect changes in an input element in real-time without waiting for the element to lose focus?
6.3.
Can I attach multiple onchange events to the same element?
7.
Conclusion
Last Updated: Aug 5, 2024
Easy

Onchange Event in JavaScript

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The onchange event in JavaScript is a useful tool that allows you to detect when the value of an input element has been changed. It is commonly used with form elements like text inputs, checkboxes, radio buttons, and select dropdowns. 

Onchange Event in JavaScript

In this article, we will discuss the onchange event in detail, like its syntax and how it works, with proper examples.

Syntax of Onchange Event in JavaScript

To use the onchange event in JavaScript, you need to attach it to an input element using the following syntax:

<input type="text" onchange="myFunction()">


In this example, the onchange attribute is added directly to the input element. When the value of the input changes, the specified JavaScript function, "myFunction()", will be called.

Alternatively, you can attach the onchange event using JavaScript code:

document.getElementById("myInput").onchange = function() {
  // Code to be executed when the value changes
};


In this approach, you first select the input element using its ID ("myInput") and then assign a function to the onchange property. The code inside the function will be executed whenever the value of the input changes.

How does Onchange Event in JavaScript Work?

The onchange event is triggered when the value of an input element is modified and the element loses focus. 

Let’s see how it works:

1. The user interacts with an input element, such as typing into a text field or selecting an option from a dropdown.


2. The user then moves the focus away from the input element, either by clicking outside the element or pressing the Tab key to move to the next form element.


3. At this point, the onchange event is triggered, and the associated JavaScript function is executed.


4. The function can access the updated value of the input element using the element's value property. For example:

var newValue = document.getElementById("myInput").value;


5. The function can perform any desired actions based on the new value, such as validating the input, updating other parts of the page, or sending the data to a server.
It's important to note that the onchange event is not triggered continuously as the user types or makes changes. It only fires when the element loses focus after a change has been made. If you need to capture changes in real-time, you can use other events like oninput or onkeyup instead.

Examples

Example 1: Text Input

<input type="text" id="name" onchange="greet()">
<script>
function greet() {
  var name = document.getElementById("name").value;
  alert("Hello, " + name + "!");
}
</script>

Example 2: Select Dropdown

<select id="color" onchange="changeColor()">
  <option value="red">Red</option>
  <option value="blue">Blue</option>
  <option value="green">Green</option>
</select>

<script>
function changeColor() {
  var selectedColor = document.getElementById("color").value;
  document.body.style.backgroundColor = selectedColor;
}
</script>

Example 3: Checkbox

<input type="checkbox" id="subscribe" onchange="toggleSubscription()">
<label for="subscribe">Subscribe to Newsletter</label>
<script>
function toggleSubscription() {
  var subscribeCheckbox = document.getElementById("subscribe");
  if (subscribeCheckbox.checked) {
    alert("You have subscribed to the newsletter!");
  } else {
    alert("You have unsubscribed from the newsletter.");
  }
}
</script>


Note: Remember to use the JavaScript code within `<script>` tags or in a separate JavaScript file linked to your HTML document.

Browser Compatibility

The onchange event is widely supported across different web browsers. It works consistently in modern browsers such as:

- Google Chrome
 

- Mozilla Firefox
 

- Apple Safari
 

- Microsoft Edge
 

- Internet Explorer 9 and above


However, it's always a good practice to test your code in multiple browsers to ensure compatibility and address any browser-specific issues that may arise.

If you need to support older browsers or handle browser differences, you can consider using JavaScript libraries like jQuery, which provide cross-browser compatibility wrappers for event handling.

Just remember, that the onchange event is specific to form elements like input, select, and textarea. If you need to detect changes in other types of elements, you may need to use different events or techniques.

Frequently Asked Questions

Can the onchange event be used with other form elements besides input, select, and textarea?

No, the onchange event is specifically designed to work with input, select, and textarea elements. For other elements, you may need to use different events like onclick or onmouseup.

Is it possible to detect changes in an input element in real-time without waiting for the element to lose focus?

Yes, you can use the oninput event instead of onchange. The oninput event is triggered whenever the value of an input element changes, allowing you to detect changes in real-time.

Can I attach multiple onchange events to the same element?

No, you can only attach one onchange event to an element. If you need to perform multiple actions, you can combine them within a single event handler function or use event listeners instead.

Conclusion

In this article, we have learned about the onchange event in JavaScript and how it allows us to detect when the value of an input element has been modified. We discussed the syntax of attaching the onchange event to form elements and understood how it works in the background.  we saw how the onchange event can be used with text inputs,with the different type of examples like, select dropdowns, and checkboxes to perform various actions based on user input. In the last, we discussed browser compatibility and alternative events for real-time input detection.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass