How can I trigger an onchange event manually in javascript?

In JavaScript, you can manually trigger an onchange event using the dispatchEvent() method with a custom Event object. This is useful for programmatically simulating user interactions.

Basic Setup

First, let's create an input element with a change event listener:

<input id="test" type="text"/>

<script>
document.querySelector('#test').addEventListener('change', () => {
    console.log("Changed!");
});
</script>

Method 1: Using Event Constructor

Create a new Event object and dispatch it on the target element:

<input id="example1" type="text" value="Initial value"/>
<button onclick="triggerChange1()">Trigger Change</button>

<script>
document.querySelector('#example1').addEventListener('change', () => {
    console.log("Change event fired!");
});

function triggerChange1() {
    const e = new Event("change");
    const element = document.querySelector('#example1');
    element.dispatchEvent(e);
}
</script>
Change event fired!

Method 2: Using CustomEvent for More Control

For more advanced scenarios, use CustomEvent to pass additional data:

<input id="example2" type="text"/>
<button onclick="triggerCustomChange()">Trigger Custom Change</button>

<script>
document.querySelector('#example2').addEventListener('change', (event) => {
    console.log("Custom change triggered");
    if (event.detail) {
        console.log("Custom data:", event.detail.message);
    }
});

function triggerCustomChange() {
    const customEvent = new CustomEvent("change", {
        detail: { message: "Manually triggered!" }
    });
    document.querySelector('#example2').dispatchEvent(customEvent);
}
</script>
Custom change triggered
Custom data: Manually triggered!

Practical Example

Here's a complete example showing how to trigger change events programmatically:

<select id="dropdown">
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
</select>
<button onclick="simulateSelection()">Simulate Selection</button>

<script>
document.querySelector('#dropdown').addEventListener('change', (event) => {
    console.log("Selected:", event.target.value);
});

function simulateSelection() {
    const dropdown = document.querySelector('#dropdown');
    dropdown.value = 'option2';  // Change the value
    
    // Trigger the change event
    const changeEvent = new Event('change', { bubbles: true });
    dropdown.dispatchEvent(changeEvent);
}
</script>
Selected: option2

Key Points

  • dispatchEvent() method triggers events programmatically
  • new Event("change") creates a basic change event
  • CustomEvent allows passing additional data via the detail property
  • Set bubbles: true if you need the event to bubble up the DOM

Conclusion

Use dispatchEvent() with new Event("change") to manually trigger onchange events. This technique is essential for testing and programmatic form interactions.

Updated on: 2026-03-15T23:18:59+05:30

28K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements