Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. javascript events

javascript events

Scheduled Pinned Locked Moved Web Development
questionjavascripthtml
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    izotov
    wrote on last edited by
    #1

    Hi, I have a small HTML with a button and a handler object which is likely to catch the button's onclick event. When I try to click the button it says "this.button.id is null or not an object". If I do onclick="objHelper.buttonClick" in the "input type=button" tag instead it works fine. What is the right solution? Here is my code: function helper() { var button; this.setButton = function(tobjButton) { this.button = tobjButton; this.button.onclick = this.buttonClick; }; this.buttonClick = function() { alert(this.button.id); }; } var objHelper = new helper(); function init() { objHelper.setButton(document.all("btnButton")); } Thanks: (K)

    M 1 Reply Last reply
    0
    • I izotov

      Hi, I have a small HTML with a button and a handler object which is likely to catch the button's onclick event. When I try to click the button it says "this.button.id is null or not an object". If I do onclick="objHelper.buttonClick" in the "input type=button" tag instead it works fine. What is the right solution? Here is my code: function helper() { var button; this.setButton = function(tobjButton) { this.button = tobjButton; this.button.onclick = this.buttonClick; }; this.buttonClick = function() { alert(this.button.id); }; } var objHelper = new helper(); function init() { objHelper.setButton(document.all("btnButton")); } Thanks: (K)

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, When I try to click the button it says "this.button.id is null or not an object" At the time the buttonclick function executes, the this keyword refers to the button object, not the objHelper. And the button object certainly does not contain any button member, so the this.button is undefined, then it should raise the error when you are accessing the this.button.id. If I do onclick="objHelper.buttonclick" in the "input type=button" tag instead it works fine Now you invoke the function buttonclick of the objHelper object, so the this keyword in the function should refer to the objHelper, and as a result it should be working as you expected. To work around, you can simply change a bit your sample code:

      this.buttonclick = function()
      {
      //alert(this.button.id);

      alert(this.id);
      

      };

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups