Results 1 to 10 of 10

Thread: Return value from Javascript function

  1. #1
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default Return value from Javascript function

    Not sure what is the level of this question, but any help would be much appreciated.

    I have 2 Javascript functions. One of them takes a value as an input. I want to return that same value from second function without passing it from first function to second function. I guess it can be done through Global variable, but not sure how it is done. I dont want to give any input for my second function, i.e. display().

    Javascript:

    <script language="JavaScript" type="text/javascript">

    var p;
    function a(imgsrc){

    p = imgsrc;

    }

    function display(){
    return p;
    }

    </script>

    HTML:

    <a onClick="a('imagesource');">Link</a>

    I'm displaying variable return from display() function in HTML (as shown below)

    <script type="text/javascript">
    document.write(display())
    </script>


    Whenever Link is clicked, I want 'p' variable to be updated.

    Thanks in advance!!!!!!!
    Last edited by zomb1e; 08-02-2012 at 12:57 PM.

  2. #2
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Hiya,
    Two questions here,
    One, Why can you not have the p variable in the second function?
    Two, you haven't actually referenced either of the functions in the onclick
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  3. The Following User Says Thank You to bernie1227 For This Useful Post:

    zomb1e (07-30-2012)

  4. #3
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default

    Thanks four your response.

    For your first question, I dont want to pass p variable in display() function coz in that case return value will be same as input. I want to use display() function only for returning 'p' value purpose which will be passed in a() function.
    So basically I want to use value passed in a() function to be used as variable which will be returned from display() function.

    For your second question, I dont understand about referencing. Is it necessary to reference your function. I guess I'm calling a() function through OnClick. And display() function is only being used to return a value which is fianlly displayed on the webpage.
    Can you please correct me if I'm wrong. Thanks!!

  5. #4
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Also -

    Please use the forum's bbcode tags to make it more readable:

    for php code............[php] <?php /* code goes here */ ?> [/php]
    for html...............[html] <!-- markup goes here -->.....[/html]
    for js/css/other.......[code] code goes here................[/code]

  6. The Following User Says Thank You to keyboard For This Useful Post:

    zomb1e (07-30-2012)

  7. #5
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Try:
    Code:
    <script language="JavaScript" type="text/javascript">	
           function a(imgsrc){
                
    	var p = imgsrc;
    				
            }
     
            function display(){
    	document.write(p);
            }
    						
    </script>
    Code:
    <a onClick="display();">Link</a>
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  8. The Following User Says Thank You to bernie1227 For This Useful Post:

    zomb1e (07-30-2012)

  9. #6
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default

    Hey I like this approach. Also it will solve my problem as we are not passing anything through display() function and desired value gets printed out by directly calling display().

    BUT

    This code is not working for me. Was it working for you?
    I tried it in a different way though. I called the function display() directly rather than calling it through onClick. But its not working.

    Code:
    <script type="text/javascript">
    	display();
    </script>

  10. #7
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Bernie's code won't work because your trying to call function display before you've called function a (the p variable is never set).

    Also, you said that when you click on the link, you want the p variable to be updated... his above script doesn't do that...

    This script should do what your asking..... maybe

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script language="javaScript" type="text/javascript">	
    function a(imgsrc){       
    	p = imgsrc;		
    	display();
    }
    function display(){
    	document.write(p);
    }					
    </script>
    </head>
    <body>
    <a onclick="a();">Link</a>
    </body>
    </html>

  11. The Following User Says Thank You to keyboard For This Useful Post:

    zomb1e (07-31-2012)

  12. #8
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Ah, that looks about right, so your calling the actuall display function when you're initializing the variable.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  13. The Following User Says Thank You to bernie1227 For This Useful Post:

    zomb1e (07-31-2012)

  14. #9
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default

    Thanks a lot everyone!!!!

    Its working now.

    U guys ROCK!!!!!!

  15. #10
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    One more thing -

    If this thread is finished, please set it to resolved.
    You can do this by editing the first post within the thread - Pressing go advanced - Then where it says no prefix, selecting resolved then save.

  16. The Following User Says Thank You to keyboard For This Useful Post:

    zomb1e (08-02-2012)

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
  •