www.webdeveloper.com
Results 1 to 2 of 2

Thread: sending vars to a function on DOM event

  1. #1
    Join Date
    Oct 2006
    Posts
    3

    sending vars to a function on DOM event

    Hi

    I don't have access to the body of the page, only to it's javascript.
    I need to call a function when a user pastes something on a certain element.
    I need to send a var to that function.

    this is what I came up with:

    Code:
    var myVar='342';
    
    myElement.onpaste = function(myVar) {
    	alert(myVar);
    }
    "myVar" in the alert comes in as "undefined", what am I doing wrong ?

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,755
    myVar is a global variable, so you don't need to include that in the function definition:
    Code:
    var myVar='342';
    
    myElement.onpaste = function() {
    	alert(myVar);
    };
    Or if you do not want to use global variables, try out function closures:

    Code:
    function getPasteHandler() {
      var handler = arguments[0];
      var args = Array.prototype.slice.call(arguments, 1);
    
      return function() {
        return handler.apply(this, args);
      };
    }
    
    myElement.onpaste = getPasteHandler(function(a, b) {
      alert(a); // alerts 1
      alert(b); // alerts 2
    }, 1, 2);

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles