SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to pass parameters

    I want to pass two parameters on button "b" click. how to pass parameters on b.onclick = test function?
    function test(x, y){
    alert(y);
    }

    var b = document.getElementById('b');

    b.onclick = test; //how to pass x and y parameter?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,080
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by aryandelhi View Post
    I want to pass two parameters on button "b" click. how to pass parameters on b.onclick = test function?
    b.onclick = function() { test(x,y); }

    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    b.onclick = function() { test(x,y); }

    Is that the only way? Can't we call "test" directly with params?

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,080
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by aryandelhi View Post
    Is that the only way? Can't we call "test" directly with params?
    No, because if you call it directly it is executed directly.

    try this:
    Code JavaScript:
    function test(x, y){
    alert(y);
    }
     
    var b = document.getElementById('b');
     
    b.onclick = test(0,"Hello");

    You will directly see a popup with "Hello", because you are calling the function. using b.onclick=function() {text(x,y);} doesn't have this problem, since you are defining the function, not calling it.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by aryandelhi View Post
    I want to pass two parameters on button "b" click. how to pass parameters on b.onclick = test function?
    Getting back to the original question, you can pass the parameters to a function, that returns a separate function. That returned function will retain access to the originally passed variables.

    Code javascript:
    function test(x, y) {
        return function () {
            alert(y);
        }
    }
     
    var b = document.getElementById('b');
    b.onclick = test(x, y);
    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
  •