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

  • Home
  • SEARCH
  • 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 6651917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:04:08+00:00 2026-05-26T01:04:08+00:00

On this page: https://developer.mozilla.org/en/DOM/element.addEventListener It is warned that the value of this will be

  • 0

On this page: https://developer.mozilla.org/en/DOM/element.addEventListener

It is warned that the value of this will be different when calling the modifyText() function when using addEventListener() as opposed to applying the event using onclick='' directly in the HTML. In the page linked above, note the first example (actually it’s the second), and then the following section titled ‘The value of this within the handler‘.

I decided to test this, but cannot find a difference. What am I doing wrong?

addeventlistener2.html:

<html>
<head>
<title>DOM Event Example 1</title>
<style type="text/css">
  #t { border: 1px solid red }
  #t1 { background-color: pink; }
</style>
<script type="text/javascript">

  // Function to change the content of t2
  function modifyText(new_text) {
    var t2 = document.getElementById("t2");
    t2.innerHTML = new_text + '<br />this: <b>' + this + '</b>';
  }

  // Function to add event listener to t
  function load() {
    var el = document.getElementById("t");
    el.addEventListener("click", function(){modifyText("body onload")}, false);
  }

</script>
</head>
<body onload="load();">

<p>has onload in body.</p>

<table id="t">
  <tr><td id="t1">one</td></tr>
  <tr><td id="t2">two</td></tr>
</table>

</body>
</html>

addeventlistener2.html:

<html>
<head>
<title>DOM Event Example 2</title>
<style type="text/css">
  #t { border: 1px solid red }
  #t1 { background-color: pink; }
</style>
<script type="text/javascript">

  // Function to change the content of t2
  function modifyText(new_text) {
    var t2 = document.getElementById("t2");
    t2.innerHTML = new_text + '<br />this: <b>' + this + '</b>';
  }

</script>
</head>
<body>

<p>has onclick in table:</p>

<table id="t" onclick='modifyText("boo!")'>
  <tr><td id="t1">one</td></tr>
  <tr><td id="t2">two</td></tr>
</table>

</body>
</html>
  • 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-05-26T01:04:08+00:00Added an answer on May 26, 2026 at 1:04 am

    In the addEventListener example you are wrapping modifyText inside an other function. All the this magic will happen to that outer function and modifyText won’t see any of it.

    Try passing modifyText directly to addEventListener

    node.addEventListener('click', modifyText);
    //and make sure to change modifyText to receive no parameters or it wont work :) 
    

    Or use the power of closures if you still want to be able to choose the parameter:

    function modifyText(new_text) {
        return function(){
            var t2 = document.getElementById("t2");
            t2.innerHTML = new_text + '<br />this: <b>' + this + '</b>';
        };
    }
    
    //addEventListener(modifyText('txt'))
    //versus
    //onclick=modifyText('txt')()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking at this page: http://developer.android.com/guide/basics/what-is-android.html The section about Applicatiosn says that android
I've spent a long time trying different things to get this to work but
I'm one of the developers of TryAgain, a Firefox add-on, that displays a custom
For some reason the script below is not working. This is the code I
I'm almost 100% sure I have ready every post on the internet that contains
I am wondering what the best way to force HTTPS authentication is. When I
I'm developing a RESTful web service with WCF and C#. The service works fine,
My project is contributed under the GPL lisence. I want to add some copyright
I'm trying to update a website for a friend on the Joomla CMS. A
I've taken some animation xml straight from the android docs, and as far as

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.