...

View Full Version : Calling a Function



hiyatran
09-27-2011, 03:59 AM
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:

<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

allister
09-27-2011, 04:11 AM
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.

DaveyErwin
09-27-2011, 04:39 AM
<!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>

Old Pedant
09-27-2011, 05:46 AM
Or this may be what you are after:


<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.

DaveyErwin
09-27-2011, 06:00 AM
I was thinking a function
named display should do
some displaying. eh ?

hiyatran
09-27-2011, 06:56 AM
Or this may be what you are after:


<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

jassi.singh
09-27-2011, 10:30 AM
Hi,

This will help you

<%@ 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>

Old Pedant
09-27-2011, 09:42 PM
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.)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum