SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to pass arguments independently or using addEventListener

    Well, I use addEventListener and I cannot (or at least don't know how) pass arguments to the function.

    Let's say we have a function:
    function warning(arg1, arg2) {alert('Argument 1: ' + arg1 + ', Argument2: ' + arg2 + '.');}

    It's possible to have:
    onclick="warning('my argument 1', 'my argument 1');" as an html attribut.

    But I think it's not posible to do it like this (still the same function):
    el.addEventListener("click", warning('my argument 1', 'my argument 1'), false);

    So how can I pass arguments to the function?

  2. #2
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rok,

    Perhaps I am not htinking my best right now, but all I could think of was to create another function that had the parameters automatically "filled in"

    el.addEventListener("click", new Function("warning('my argument 1', 'my argument 1')"), false);

    I have written an article on JavaScript OOP (Object oriented programming). IMO, I think it's a great article for any serious JavaScripter. The answer may be in there and I just can't htink of it off the top of my head.

    aDog


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
  •