SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    add multiple functions in javascript

    how could I add multiple functions in the fiollowing cases?
    Code JavaScript:
    myvar.addEventListener("change", functionx, false);
    myvar.attachEvent("onchange", functionx);

    I tried by just adding more functions after the comma but it is not working

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    You can instead trigger a different function that itself runs the multiple function you require.

    For example:

    Code javascript:
    function changeHandler(evt) {
        functionx();
        functiony();
    }
    myvar.addEventListener("change", changeHandler, false);

    If you want to pass on to those multiple functions the reference to the this keyword, and the changeHandler arguments (that being the evt variable), you can use the apply method to invoke those functions instead:

    Code javascript:
     
    function changeHandler(evt) {
        functionx.apply(this, arguments);
        functiony.apply(this, arguments);
    }
    myvar.addEventListener("change", changeHandler, false);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •