Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts

    Event Scope Problem

    I have a method of a class called add(). Within add() I set define an onkeyup event. But within the onkeyup event, I need to access class variables, but the this keyword no longer refers to the class. I need to be able to edit the variable as well. So my problem is that the code

    Code:
    add(){
    element.onkeyup=function(){
    this.errors='';
    }
    }
    v=new validation();
    v.add();
    will get the error "this.errors is undefined".

    How can I bind the this.errors parameter?

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Something like this:

    Code:
    var ref = this;
    element.onkeyup=function(){
    ref.errors='';
    }
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •