SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: function call

  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    function call

    hi,
    is there a better way than assigning myob = this;
    to use it within myDiv.onclick ? (using call() or apply() ? )

    thanks

    pete

    Code:
    function myPage() 
    {
      this.testfunction();
      var myDiv = $("myDiv");
    
      var myob = this;
      this.myDiv.onclick = function() {
        myob.testfunction();
      }
    }
    
    myPage.prototype.testfunction = function() { alert ("test"); }

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Not really, because within the onclick function's scope, this gets redefined. Why not just do this:
    Code Javascript:
    function myPage() {
      this.testfunction();
      this.myDiv = $('myDiv');
      this.myDiv.onclick = this.testfunction;
    }
    myPage.prototype.testfunction = function() {
      alert(this.id);  // 'myDiv' (only upon onclick)
    }
    ?

    Then when testfunction runs, this is the element the event happened on.


Bookmarks

Posting Permissions

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