What's event driven programming | SoloLearn: Learn to code for FREE!


What's event driven programming

5/21/2020 10:26:55 AM

Erick Mapesa Misana

2 Answers

New Answer


Event-driven programming is the use of events to trigger functions. An event maybe a touch, hold, click, release, etc. Basically you define a function which will be called each time the related event is encountered or occurs. For example, in JavaScript, for defining what to do when a button (with id 'but') is clicked, you need to use an 'Event Listener' for the 'click' event. You may also use the button's onclick attribute to call the function or assign the function to the 'onclick' DOM object member function: Let the function be defined as: function pressButton(){ console.log("Hello from the button with id 'but'"); } Linking the function with the click event of the button: 1.) Using onclick attribute in HTML: <button id="but" onclick="pressButton()">Press</button> 2.) Using onclick member function in JS: document.getElementById("but").onclick = pressButton(); 3.) Using the addEventListener function in JS: document.getElementById("but").addEventListener("click", pressButton);