
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I trigger an onchange event manually in javascript?
You can dispatch events on individual elements using the dispatchEvent method. Let's say you have an element test with an onChange event −
<input id="test" type="text"/>
Event handler −
document.querySelector('#test').addEventListener('change', () => console.log("Changed!"))
Triggering the event manually −
const e = new Event("change"); const element = document.querySelector('#test') element.dispatchEvent(e);
This will log the following −
Changed!
Advertisements