function is not defined

G

Gernot Frisch

With the following code, I get the error
Error: DoClick is not defined
Source File: file:///E:/Temp/mcd.html
Line: 1

when I click on any button.



# # #
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<title>McD Menu Calc</title>

<script type="Text/JavaScript">
var gFood = new Array;

function DoClick(name, ammount)
{
gFood[name] = gFood[name] + ammount;
document.elementbyid(name+'val').innerhtml=gFood[name];
}


function AddFood(name)
{
document.write("<b>" + name +
"<\/b>&nbsp;<button onClick='DoClick("+
name+", -1);' >Min<\/button>" +
"<span id='"+name+"val'>0<\/span>"+
"<button onClick='DoClick("+
name+", 1);' >Max<\/button><br>");
gFood[name] = 0;
}

function StartUp()
{
AddFood("BigMac");
AddFood("Pommes");
}

</script>
</head>

<body onload="StartUp();">
</body>
</html>
# # #

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
E

Evertjan.

Gernot Frisch wrote on 26 feb 2007 in comp.lang.javascript:
With the following code, I get the error
Error: DoClick is not defined
Source File: file:///E:/Temp/mcd.html
Line: 1

when I click on any button. [...]

function AddFood(name)
{
document.write("<b>" + name +
[..]
function StartUp()
{
AddFood("BigMac");
AddFood("Pommes");
} [..]
<body onload="StartUp();">

Executing document.write() when the page is loaded
invoces an implicit document.open()
that destroys the page WITH all scripting.

If there is no scripting left, there are no functions to be found.
 
E

Evertjan.

Evertjan. wrote on 26 feb 2007 in comp.lang.javascript:
Gernot Frisch wrote on 26 feb 2007 in comp.lang.javascript:


Executing document.write() when the page is loaded
invoces an implicit document.open()
that destroys the page WITH all scripting.

If there is no scripting left, there are no functions to be found.


and:

document.elementbyid(name+'val').innerhtml=gFood[name];

must be:

getElementById
innerHTML

and:


document.write("<b>" + name +
"<\/b>&nbsp;<button onClick='DoClick("+
name+", -1);' >Min<\/button>" +
"<span id='"+name+"val'>0<\/span>"+
"<button onClick='DoClick("+
name+", 1);' >Max<\/button><br>");

document.write("<b>" + name +
"<\/b>&nbsp;<button onClick='DoClick(\""+
name+"\", -1);' >Min<\/button>" +
"<span id='"+name+"val'>0<\/span>"+
"<button onClick='DoClick(\""+
name+"\", 1);' >Max<\/button><br>");

and:
var gFood = new Array;

the array is not used, but the object.


============================================

Try this, tested working in IE:

<html>
<head>
<title>McD Menu Calc</title>

<script type='text/javascript'>
var gFood = {};

function DoClick(name, ammount){
if (!gFood[name])gFood[name] = 0;
gFood[name] = gFood[name] + ammount;
document.getElementById(name+'val').innerHTML=gFood[name];
}
</script>
</head>

<body>
<script type="Text/JavaScript">
function AddFood(name){
document.write("<b>" + name +
"<\/b>&nbsp;<button onClick='DoClick(\""+
name+"\", -1);' >Min<\/button>" +
"<span id='"+name+"val'>0<\/span>"+
"<button onClick='DoClick(\""+
name+"\", 1);' >Max<\/button><br>");
}

AddFood("BigMac");
AddFood("Pommes");
</script>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top