-
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
-
Senior Member
The display function is getting called from product but your document.write calling the display function is not passing a value so the return will be undefined.
It looks more to me like theres a scope / event issue here.
Could you explain what you what the page to do and maybe we can sort out the Javascript from there. My guess is you're wanting a the product function to update a variable and then return that variable through the display function? It would be better to use a Javascript "object" if you want to preserve variable states.
-
Code:
<html>
<head>
<script type="text/javascript">
var toDisplay;
function product(a,b){
total = a*b;
toDisplay=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(toDisplay);
</script>
</body>
</html>
Couple of problems. When you return something, you need a variable for the return value (i.e. a container for the value). Then you write out the variable, not the function (which will just run again and give you nothing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|