Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    39
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Calling a Function

    I would like to call a function within a function. Is this even possible??
    When I do this I get "undefined"

    Here's my code:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function product(a,b){
       total = a*b;
       display(total);
    }
    
    function display(num){
       return num;
    }
    </script>
    </head>
    
    <body>
    <h1>This text I do NOT want to be erased.</h1>
    
    <a href="http://google.com" target="_blank">Total: </a>
    <script type="text/javascript">
    product(3,4);
    document.write(display());
    </script>
    
    </body>
    </html>
    I know you can just return in the first function, "product()" but I would like to do something else in my second function "display()" then return a value.

    thanks

  • #2
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post
    don't know what's your meaning to do this.

    Let's just see what your code has done. This line:

    document.write(display());

    We can change it to two lines:

    var result = display();
    document.write(result);

    For function display, it needs a parameter, as it is defined like:

    function display(num){
    return num;
    }

    The return value of function display is just what you have passed in.

    As you didn't passed any thing when you called this function, it returned nothing. So result is undefined, and that's why "undefined" is printed on the page.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="daveyerwin">
    <title>Untitled</title>
    <script type="text/javascript">
     function product(a,b){
       total = a*b;
       display(total);
    }
    
    function display(num){
       document.write(num);
    }
    
    
    </script>
    </head>
    <body>
    <h1>This text I do NOT want to be erased.</h1>
    
    <a href="http://google.com" target="_blank">Total: </a>
    <script type="text/javascript">
    product(3,4);
    
    </script>
    
    
    </div>
    </body>
    </html>

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,631 Times in 4,594 Posts
    Or this may be what you are after:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function product(a,b){
       total = a*b;
       return total;
    }
    
    function display(num){
       return num;
    }
    </script>
    </head>
    
    <body>
    <h1>This text I do NOT want to be erased.</h1>
    
    <a href="http://google.com" target="_blank">Total: </a>
    <script type="text/javascript">
    document.write( display( product(3,4) ) );
    </script>
    
    </body>
    </html>
    Only the code in red is changed from your original.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    I was thinking a function
    named display should do
    some displaying. eh ?

  • #6
    New Coder
    Join Date
    Jun 2010
    Posts
    39
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Or this may be what you are after:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function product(a,b){
       total = a*b;
       return total;
    }
    
    function display(num){
       return num;
    }
    </script>
    </head>
    
    <body>
    <h1>This text I do NOT want to be erased.</h1>
    
    <a href="http://google.com" target="_blank">Total: </a>
    <script type="text/javascript">
    document.write( display( product(3,4) ) );
    </script>
    
    </body>
    </html>
    Only the code in red is changed from your original.
    This is exactly what I wanted to do.
    This is so obvious, can't believe I didn't get it.
    I guess thats what happen when you're running on 4 hrs of sleep.
    tks Old Pedant

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    103
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Hi,

    This will help you
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    <script type="text/javascript">
    
        function dropValue(source) {
    
            document.getElementById('<%= destinationtextbox.ClientID %>').value = source.value;
            displayfromsecond();
        }
    
        function displayfromsecond() {
            alert(document.getElementById('<%= destinationtextbox.ClientID %>').value);
        }
    </script>
    </head>
    <body >
        <form id="form1" runat="server">
        <div>
           
    <asp:TextBox ID="sourcetextbox" runat="server" onblur="javascript:dropValue(this)"></asp:TextBox>
    <asp:TextBox ID="destinationtextbox" runat="server" ></asp:TextBox>
        </div>
        </form>
    </body>
    </html>

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,631 Times in 4,594 Posts
    Jassi: You really should *NOT* post ASP.NET code in this forum. Very very few people here are using ASP.NET. (Most seem to be using either HTML only or possibly PHP.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •