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 7889491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:06:48+00:00 2026-06-03T06:06:48+00:00

See my code http://jsfiddle.net/kxATT/3/ function byId(id) { if (id) { return document.getElementById(id); } else

  • 0

See my code
http://jsfiddle.net/kxATT/3/

           function byId(id) {
               if (id) {
                   return document.getElementById(id);
               } else {
                   return null;
               }
           }

           function setAttr(elm, attr, value) {
               elm.setAttribute(attr, value);
           }

           var app = {
               id: 12,
               fn: function () {
                   alert(this.id);
               }
           };

           function init() {
               setAttr(byId('txt'), 'onclick', 'app.fn()');
               byId('txt1').addEventListener('click', app.fn, true);
           }

When the first box is clicked,it alerts 12. And for the second box it alers txt1. Obviously in both cases although I’m calling the same function, this refers to different objects. I’ve heared that, for input elements,the element’s object itself is passed to the function and reffered as this inside the event handler function. If so why is it not true when events are added using setAttribute.

  • 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-03T06:06:50+00:00Added an answer on June 3, 2026 at 6:06 am

    When you assign a string to onclick, you’re basically creating the body of a function; the browser supplies the function, e.g. your string "app.fn()" becomes:

    function onclickHandlerCreatedByBrowser(event) {
        app.fn();
    }
    

    When you assign a function reference via addEventListener, as with any time you use a function reference with JavaScript, it’s just a function reference; there is no object information. this in JavaScript is set entirely by how a function is called, not where it’s defined.

    If you’re using an ECMAScript5-enabled environment (or using an ES5 shim), you can solve this using Function#bind:

    byId('txt1').addEventListener('click', app.fn.bind(app), true);
    

    Otherwise, you can solve it by supplying the function that the browser provides in the string case yourself:

    byId('txt1').addEventListener('click', function() {
        app.fn();
    }, true);
    

    More reading:

    • Mythical methods
    • You must remember this
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my code: http://jsfiddle.net/WCP5f/2/ If you open it with Chrome, you will see
I'm running this code in Firefox 11 on Windows 7. (See http://jsfiddle.net/QStkd/ ). $('<div><input
I've got a toggle menu, please see http://jsfiddle.net/Wp2em/41/ for code and functions. On the
See my code: http://jsfiddle.net/NxNXJ/5 The star should be the same size as the sun
Here you can see the code: http://jsfiddle.net/LQSK3/1/ I can't get display: inline; working there
You can see the code live here: http://jsfiddle.net/z3xV3/50/ My HTML: <div id=slider></div> <input id=sliderValue
Please see my source code at: http://jsfiddle.net/rAmSG/9/ The blue item should be dropped at
Hey all. I have the following code: http://jsfiddle.net/g7Cgg/ As you can see, there are
See this code example: http://jsfiddle.net/Z2BMK/ Chrome/IE8 look like this Firefox looks like this My
Please see the code here: http://jsfiddle.net/wQpTu/1/ For some reason, it always returns false on

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.