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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:20:41+00:00 2026-06-01T19:20:41+00:00

I have a code like this: var Obj = { el : document.getElementById(elementId), doSomething

  • 0

I have a code like this:

var Obj = {
  el : document.getElementById("elementId"),

  doSomething : function(){ this.el.property = "value" }
};

Obj.el.addEventListener('click', Obj.doSomething);

but the element this.el is undefined when the click event is fired ?
What causes this to happen?

NOTE: I don’t know how to break the lines in the code in this editor, that’s why it’s in a single line.

  • 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-01T19:20:42+00:00Added an answer on June 1, 2026 at 7:20 pm

    In JavaScript (unlike most other languages with similar syntax), this is defined entirely by how a function is called, not where it’s defined. The way event handlers are called, this within your function doesn’t refer to your object.

    You can correct it in three ways:

    1. If you’re targeting ES5-enabled browsers or including an ES5 “shim”, use Function#bind:

      var Obj = {
        el : document.getElementById,
      
        doSomething : function(){ this.el.property = "value" }
      };
      
      Obj.el.addEventListener('click', Obj.doSomething.bind(Obj));
      
    2. Otherwise, use a closure:

      var Obj = {
        el : document.getElementById,
      
        doSomething : function(){ this.el.property = "value" }
      };
      
      Obj.el.addEventListener('click', function(event) {
          return Obj.doSomething(event);
      });
      
    3. As RobG pointed out in the comments, because you have only one Obj object (rather than having a constructor function or something that will create more than one as necessary), you could just refer to that object using its variable (Obj) rather than this:

      var Obj = {
        el : document.getElementById,
      
        // Obj instead of this ---v
        doSomething : function(){ Obj.el.property = "value" }
      };
      
      Obj.el.addEventListener('click', Obj.doSomething);
      

      That only works when the object is a one-off within the scope in which it’s defined (which is true for your Obj example). If the only reference you have to it were this (e.g., you didn’t have Obj), you couldn’t do this and would want one of the prior two options.

    More:

    • Mythical methods
    • You must remember this

    Note that your el property is suspect, though. You’ve defined it as

    el : document.getElementById,
    

    …which means that el now points to the function getElementById. That’s probably not what you meant to do. Presumably you meant el to refer to a DOM element that you retrieved via getElementById, e.g.:

    el : document.getElementById("someIdValue"),
    

    More here.

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

Sidebar

Related Questions

I have some code like this: var content = document.getElementById('myDivId'); function MyFunction() { alert(content.style.height);
I have got code like this var challegneListener; $(document).ready(function(){ var challegneListener = setInterval(challengeListenerBot(),5000); });
I have some code like this: function switch_tabs(obj) { $('.tab-content').hide(); $('.tabs a').removeClass(selected); var id
I have code like this var MyObj = { f1 : function(o){ o.onmousedown =
I have code that looks like this: var baseClass = function() { // CODE
I have some code like below $(html).on(change, input['data-href'], function() { var href, obj, params;
I have a simple code like this: var name = 'line1'; var obj =
I have this code: $(document).ready(function() { var url = https://graph.facebook.com/search?q=cinema&type=post; $.ajax({ type: POST, url:
For example I have this plugin code: jQuery.fn.keys = function(obj) { var keys =
I have code like this: var newMsg = new Msg { Var1 = var1,

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.