SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    May 2005
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    newbie -- pass variable value

    Hi all,
    yes, new to this and I may have missed the answer somewhere,

    I would like to get a value from one function to another

    it would look something like this



    function startLocation() {
    var passThisValue = "moving variable";
    }

    function endLocation() {
    alert(passThisValue);
    }


    I would like the alert to say: moving variable


    I know it's something simple, but haven't been able to get it.
    thanks for the help!

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Variables declared in a function are only visible within that function. For two functions to access the same variable, the variable must be declared outside both functions.

    Code:
    var passThisValue;
    
    function startLocation() {
         passThisValue = "moving variable";
    }
    
    function endLocation() {
         alert(passThisValue);
    }

  3. #3
    SitePoint Enthusiast Grayson's Avatar
    Join Date
    Jun 2008
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    function start()
    {
      this.varName = 'moving variable';
      this.endReference = function ()
      {
        end(this.varName);
      }
    }
    function end( varName )
    {
      alert(varName);
    }
     
    start.endReference();


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
  •