#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    38
    Rep Power
    2

    Passing javascript variable to another function


    Hello all. I am trying to pass a variable to another function. It passes ok but the second function executes as well. I would like the variable to pass to the second function but not execute the second function. I have an onClick setup to run that at a later time. How can I do this?

    This is my code...

    Code:
    function myFunction1 () {
    var rand = "5";
    myFunction2(rand);
    }
    
    function myFunction2(rand) {
    alert(rand); //to make sure it is passed
    //other function stuff....
    }
    I thought about putting a break at the top of the second function but that would also stop it when called from the button click.

    Thanks for all help!
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,122
    Rep Power
    4258
    It seems you've figured out what your problem is, but you've oversimplified it so we can't give you specific advice. How are you assigning the click handler?

    No, using a break statement would not work. Those are only for controlling loop and switch execution. JavaScript does not have a pause statement either.

    The solution most likely involves using a closure:
    http://blog.morrisjohns.com/javascri...es_for_dummies
    http://www.jibbering.com/faq/faq_notes/closures.html
    http://robertnyman.com/2008/10/09/ex...-and-closures/
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    38
    Rep Power
    2
    Here is my onclick code...

    Code:
    <a href="#" id="buttonwidth1" data-role="button" data-inline="true" data-mini="true" onClick="myFunction2();" >Click Me</a>
    Ultimately I want the variable 'rand' from myFunction1 to be passed to myFunction2 when the button is clicked. I have used the following code to add a value to a form element but since this isn't a form I am not sure how I would do it.

    Code:
    document.answerform.number.value=rand;
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    38
    Rep Power
    2
    Ok, so I figured it out. I declared my variable globally. What threw me off was I tried that before but still had it declared inside my first function as well.

    var rand = .....

    I took out the 'var' from inside the function and it works now.

    rand = ....

IMN logo majestic logo threadwatch logo seochat tools logo