Skip to content
Fix Code Error

How to trigger event in JavaScript?

March 13, 2021 by Code Error
Posted By: Anonymous

I have attached an event to a text box using addEventListener. It works fine. My problem arose when I wanted to trigger the event programmatically from another function.

How can I do it?

Solution

You can use fireEvent on IE 8 or lower, and W3C’s dispatchEvent on most other browsers. To create the event you want to fire, you can use either createEvent or createEventObject depending on the browser.

Here is a self-explanatory piece of code (from prototype) that fires an event dataavailable on an element:

var event; // The custom event that will be created
if(document.createEvent){
    event = document.createEvent("HTMLEvents");
    event.initEvent("dataavailable", true, true);
    event.eventName = "dataavailable";
    element.dispatchEvent(event);
} else {
    event = document.createEventObject();
    event.eventName = "dataavailable";
    event.eventType = "dataavailable";
    element.fireEvent("on" + event.eventType, event);
}
Answered By: Anonymous

Related Articles

  • Tkinter Custom Rectangle widget
  • Why does my convolutional model does not learn?
  • How do I keep only the first map and when the game…
  • Multiple context menu in a single qTableView pyqt5
  • My Button Functions Are Not Working in my Python code
  • Trouble understanding behaviour of modified VGG16…
  • How do I programmatically change the parent of a layout
  • Layout doesn't expand more than initial size it was…
  • How to "properly" create a custom object in JavaScript?
  • Matplotlib plot's title is missing for unknown…
  • How can I connect a signal to different slots…
  • Resizing an image in an HTML5 canvas
  • Combining pyOSC with pyQT5 / Threading?
  • how to use canvas in JavaScript flappy bird code
  • How to monitor a filtered version of a metric in…
  • How does JavaScript .prototype work?
  • For-each over an array in JavaScript
  • Pygame Curve Movement Problem How To Fix?
  • What is the 'new' keyword in JavaScript?
  • Python-coded neural network does not learn properly
  • How to use QThread() within class of QWidget function?
  • Trouble using dispatchEvent using Polymer and Firefox
  • DQN Pytorch Loss keeps increasing
  • collision detection from picturebox in a list c#
  • How to return a negative fraction when subtracting…
  • QGraphicsItem don't change pen of parent when chaning child
  • Trying to append a Polymer element in another…
  • Navbar not filling width of page when reduced to mobile view
  • Polymer trigger function in child element using…
  • Kivy WebView Error: Cannot add to window, it already…
  • How to get the updated entry string from a toplevel…
  • How to move the player across a one background image?
  • How do i update a javascript variable as its value changes?
  • How do I create a radio button that accept user input text?
  • iOS UICollectionViewCell resizable dashed border
  • In CSS Flexbox, why are there no "justify-items" and…
  • Why doesn't my scrolbar appear after clicking the…
  • How to dynamically add widgets to a layout after a…
  • How not to get a repeated attribute of an object?
  • Create a Custom Widget with QListWidget,QLable and…
  • Why is UICollectionViewDiffableDataSource reloading…
  • PyQt5 add text over browser page
  • Can i add conditional events on svelte components?
  • Start a Thread Timer directly
  • Adding animation to QPushbutton enterEvent and exitEvent
  • Aurelia Binding Change.delegate Not Firing (JQueryUI…
  • Why Do I get a Stack Overflow error when using…
  • How to simulate a mouse click using JavaScript?
  • How do I switch two players in tic-tac-toe game in Python?
  • Unknown difference between logic of OOP code…
  • How does the @property decorator work in Python?
  • TensorFlow 2.0 : ValueError - No Gradients Provided…
  • What does this symbol mean in JavaScript?
  • JavaScript gives NaN error on the page but variable…
  • addEventListener vs onclick
  • Polymer paper-dialog: how can I know when an…
  • Onclick event not happening when slick slider is…
  • I am making a browser In pyqt5, but I don't know how…
  • Polymer 1.0 Trying to make a splitter which works…
  • setTimeout function not working : javascript
  • MPRemoteCommandCenter error: the track is played…
  • How to store the last checked item as default, when…
  • How can I require at least one checkbox be checked…
  • Rust: implementing an iterator from a vector of…
  • Best way to extract messy HTML tables using BeautifulSoup
  • Pure JavaScript equivalent of jQuery's $.ready() -…
  • wxpython do action before closing wx.EVT_CLOSE
  • Tkinter understanding mainloop
  • Why do we use __init__ in Python classes?
  • Coffeescript class extend more bloat than Backbone extend
  • Make the whole gridLayoutWidget clickable
  • How to create a game over screen for a basic HTML/JS game?
  • How can I pass a wct test while rearranging children…
  • PyQT5: how to automatically align the widget in QGridLayout?
  • How to obtain the Minimum Size/Fixed Size of the…
  • Moving cursor to first entry box pyqt5
  • ROS topic is not published yet
  • Aurelia customAttribute not working
  • How do I include a JavaScript file in another…
  • Adding eventListener to blur event on custom component?
  • TypeScript metadata reflection references other…
  • Javascript validate all checkboxes are selected
  • Rust lifetime confusion
  • Call a method from a method of another class (Nested Class)
  • How do I add a back button and a forward button in…
  • ImportError: No module named dateutil.parser
  • Deep Q Learning - Cartpole Environment
  • Django - update inline formset not updating
  • How can I wrap all BeautifulSoup existing…
  • insert tables in dataframe with years from 2000 to…
  • Vue.js, How to get a list of latitude and longitude…
  • TypeError: Cannot convert a symbolic Keras…
  • Smart way to truncate long strings
  • Pygame Enemy not showing on screen
  • EXP system for adjusting instance attributes in…
  • How to convert value of type 'ImagePickerView' to…
  • jQuery Mobile: document ready vs. page events
  • Flappy bird code not working - JavaScript
  • Active tab issue on page load HTML
  • CSS Input with width: 100% goes outside parent's bound

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

jquery change class name

Next Post:

Altering column size in SQL Server

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error