Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:07:45+00:00 2026-06-07T05:07:45+00:00

<div id="myDiv" onclick="MyFunc()"></div> document.getElementById("myDiv").onclick = MyFunc; document.getElementById("myDiv").addEventListener("click", MyFunc, false) Now, what is the difference

  • 0
  1. <div id="myDiv" onclick="MyFunc()"></div>

  2. document.getElementById("myDiv").onclick = MyFunc;

  3. document.getElementById("myDiv").addEventListener("click", MyFunc, false)

Now, what is the difference between these 3 ways of attaching an event to a DOM element (ignoring the fact that 3 won’t work on IE)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:07:47+00:00Added an answer on June 7, 2026 at 5:07 am

    Methods A and B

    These are known as DOM level zero events and are somewhat old school. Method A declares it in-line in your HTML (bad) where as method B does the same thing but centrally, in your JS.

    With method A, the attribute value is a string of valid JS that, on firing, will be evaluated (also bad). Due to the position in which the event is being bound, this means any functions referenced in this string must be global (or globally accessible methods). With method B, the event is bound centrally, in your JS, rather than inline.

    The main caveat with them, aside from being outdated and simplified, is that you can bind only one kind of event per element. If you attempt to bind two click event handlers to the same event handler with this mechanism, the first will be forgotten. This stands to reason, since you are simply overwriting an element attribute.

    Method C

    addEventListener is the standard for attaching events. For a long time, IE didn’t support this, favouring its equivalent attachEvent method. Some differences between them include:

    • attachEvent does not allow capturing of events (param 3 of addEventListener allows this)
    • with attachEvent, the event object (i.e. the object that stores information about the fired event) is accessed on window.event, whereas with addEventListener it is forwarded as the only argument to the callback
    • with attachEvent, event names must be prefixed with on, e.g. onClick. addEventListener requires simply click
    • with addEventListener, the this keyword inside the callback points to the element that triggered the event. In attachEvent you have to decipher this yourself by extrating the element from properties within the event (window.event) object

    IE9 came into line and supports addEventListener.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a <div name="myDiv">0</div> . I try to write a test that myDiv
This is my code : HTML <div class="myDiv">text under link!</div> <a class="myLinkTransparent" href="#">&nbsp;</a> <a
In my App I have the following: $("#displayPanel div").live("click", function(){ $(this).css({'background-color' : 'pink', 'font-weight'
I've got some elements like <div id="myDiv"> <p><input type="checkbox" />text</p> <p>special text</p> <p><input type="checkbox"
I know I can do this: $("#my_div").live("click", function() { alert("My Div was clicked"); }
I need to have a for loop inside my jQuery. Example: for(i=0;i<counter;i++) { $("div"+i+"").click(function(){//some
Wondering why I can't get document.getElementById("my_div").innerHTML to update the DOM when I re-assign the
Relevant snippet from test.html: <div id="seat_31F_vacant" class="seatVacant" onclick="UpdateHost(this);Common.DoPostBack('lbtPostBack','31F');" onmouseover="Seat_onMouseOver(this)" onmouseout="Seat_onMouseOut(this)">F</div> Now consider this ruby
'<div id="'+div_id+'" class="widget" style="height:60px;width:110px">\n\ <div class="widget-head ui-widget-header" style="cursor:move;height:20px;width:130px">'+ '<span id="'+span_id+'" style="float:right; cursor:pointer" class="dialog_link ui-icon
My HTML: <div id="parent"> <div id="child">cx</div> </div> When I use jQuery $('#parent').mouseout(function(){ //something here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.