最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

event vs eventhandler in javascript - Stack Overflow

programmeradmin0浏览0评论

My teacher says that onclick in javascript is an event and an event handler. But i'm not able to agree with her, I find them to be different but can't distinguish properly.Have done a ton of google search , couldn't find anything relevant. So somebody please distinguish between event and event handler.

My teacher says that onclick in javascript is an event and an event handler. But i'm not able to agree with her, I find them to be different but can't distinguish properly.Have done a ton of google search , couldn't find anything relevant. So somebody please distinguish between event and event handler.

Share Improve this question asked May 8, 2017 at 6:06 GARIMA JAINGARIMA JAIN 611 silver badge7 bronze badges 2
  • 1 click is the event, onclick is the handler.. what's the question btw? – Mr. Alien Commented May 8, 2017 at 6:09
  • @Mr.Alien-then what is the function which is given after =sign in " " eg:<button onclick="myfunc()";> ,I supposed the function to be an event handler. What do you say [email protected] – GARIMA JAIN Commented May 8, 2017 at 6:58
Add a ment  | 

5 Answers 5

Reset to default 4

According to documentation at https://developer.mozilla.

Event:

Events are sent to notify code of things that have taken place. Each event is represented by an object which is based on the Event interface, and may have additional custom fields and/or functions used to get additional information about what happened. Events can represent everything from basic user interactions to automated notifications of things happening in the web page.

Event Handler:

The function or lines of code that do something upon an event fire are known as event handlers.

For example:

click is an event that is fired when something is clicked.

onclick is an event handler that does something when the click event occurs.

<button onClick="alert('You clicked me');">Click me to fire a click event</button>

In the above example when the click event occurs on the button the eventhandler (onClick) does a job which is to alert and show a message.

Event handlers can also be attached to certain events like in the example below:

document.getElementById('sampleParagraph').addEventListener("click", function(){
  //I am the event handler Function
  alert("A click event occured on the paragraph");
});
<p id="sampleParagraph">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
</p>

In the example above we attached an event handler to the paragraph which on click event shows you an alert.

Event signifies that something has happened.Event handler is when that event has happened,what to do i.e list of things to do when that particular event has happened.

What is onclick event? When the user clicks on a button or any element,if the onclick attribute is registered,automatically an event is registered and the function you specify when that event has occured( event handler ) will be called with the event object being passed (optional).

It's just terminology for different things you need to deal with considering events in javascript.

onclick it the name of an event.

When it occurs you will get an onclick event object.

The function you write to process the event is an event handler.

So when the event occurs an event object is passed to your event handler.

Event is something that can happen to your element and an event handler would be a way of defining how you want to react to that event. For example, an onclick would be an event that would happen when you click a button, now you can define a function that gets called when that event happens which would be your event handler. Reference : https://www.w3schools./js/js_events.asp

An “event” is an action that happens to HTML elements like click, mouseover, keydown etc. It is related to browser. On the other hand, “event handler” is written in JavaScript to handle the actions caused by the browser like verify user input etc.Link

发布评论

评论列表(0)

  1. 暂无评论