MrDNA Posted June 14, 2010 Share Posted June 14, 2010 Hello, and sorry to just fall in with a question.Since a few days I started to follow a javascript tut. Its quite easy. But it seems my code wont work. \When I compare it with the given example it matches if you ask me.But when I overwrite my code with the example, the example works.The code:<html> <head> <title></title> <script type="text/javascript"> function Delen() { Getal1 = window.document.Form1.Box1.value; Getal2 = window.document.Form1.Box2.value; DelenUitkomst = Getal1 / Getal2; alert(Getal1 + " ÷ "+ Getal2 + " = " + DelenUitkomst); } function Keer() { Getal1 = window.document.Form1.Box1.value; Getal2 = window.document.Form1.Box2.value; KeerUitkomst = Getal1 * Getal2; alert(Getal1 + " X " + Getal2 + " = " + KeerUitkomst); } function Min() { Getal1 = window.document.Form1.Box1.value; Getal2 = window.document.Form1.Box2.value; MinUitkomst = (Getal1 *1) - (Getal2*1); alert(Getal1 + " - " + Getal2 + " = " + MinUitkomst); } function Plus() { Getal1 = window.document.Form1.Box1.value; Getal2 = window.document.Form1.Box2.value; PlusUitkomst = (Getal1 *1) + (Getal2*1); alert (Getal1 + " + " + Getal2 + " = " + PlusUitkomst); } </script> </head> <body> <center> <form name="Form1"> <input type="text" name="Box1" value="" size=8><BR/> <input type="text" name="Box2" value="" size=8> </form> <a href="java script:DelenUitkomst()">Delen</a> <a href="java script:KeerUitkomst()">Keer</a><BR/> <a href="java script:MinUitkomst()">Min</a> <a href="java script:PlusUitkomst()">Plus</a> </center> </body></html>So what am I doing wrong? I cant find it..Thank you in advance!PS: Big fan of the w3 community. Hope to join it someday! Link to comment Share on other sites More sharing options...
thescientist Posted June 14, 2010 Share Posted June 14, 2010 two things: you shouldn't use dot notation when trying to reference elements within a document. The prefered and advisable method is using document.getElementById().I'm pretty sure this: <a href="java script:DelenUitkomst()">Delen</a> <a href="java script:KeerUitkomst()">Keer</a><BR/><a href="java script:MinUitkomst()">Min</a> <a href="java script:PlusUitkomst()">Plus</a> won't work. You would want to assign an event handler to your anchor tags, say onClick='functionName()" if you want the function to execute when someone clicks on it. Link to comment Share on other sites More sharing options...
ShadowMage Posted June 14, 2010 Share Posted June 14, 2010 I'm pretty sure this:<a href="java script:DelenUitkomst()">Delen</a> <a href="java script:KeerUitkomst()">Keer</a><BR/><a href="java script:MinUitkomst()">Min</a> <a href="java script:PlusUitkomst()">Plus</a> won't work. You would want to assign an event handler to your anchor tags, say onClick='functionName()" if you want the function to execute when someone clicks on it. Actually that does work. We use it here at work a lot, on some of our older pages. I would recommend the onclick though.Secondly, I agree with scientist on the getElementById() method and dot notation. Assign id's to your inputs and ditch the name attribute on your form (but keep it on your inputs).Thirdly, you might consider casting your values before assigning them to variables. That will eliminate the need for this sort of thing:MinUitkomst = (Getal1 *1) - (Getal2*1);To cast, just put Number() around your values. Like so:Getal1 = Number(window.document.getElementById("Box1").value);Then you could do your calculation like this:MinUitkomst = Getal1 - Getal2; Link to comment Share on other sites More sharing options...
MrDNA Posted June 14, 2010 Author Share Posted June 14, 2010 Thank you for your response.In the original code thats written fully. <a href="java script:DelenUitkomst()">Delen</a> <a href="java script:KeerUitkomst()">Keer</a><BR/><a href="java script:MinUitkomst()">Min</a> <a href="java script:PlusUitkomst()">Plus</a> Ill look into anchor points.Thank you! Link to comment Share on other sites More sharing options...
MrDNA Posted June 14, 2010 Author Share Posted June 14, 2010 @shadowmage:I thought of doing it that way. Seemed better to me. But the tut said.. so i did..Still learning. But it seems the tut is outdated..Ty. Link to comment Share on other sites More sharing options...
thescientist Posted June 14, 2010 Share Posted June 14, 2010 Actually that does work. We use it here at work a lot, on some of our older pages. I would recommend the onclick though.Always there to set me straight. You're lucky I'm not adverse to learning something new everyday... or else I might think it was something personal! Link to comment Share on other sites More sharing options...
dsonesuk Posted June 14, 2010 Share Posted June 14, 2010 function Delen()function Keer()function Min()function Plus()function names compared to called function names<a href="java script:DelenUitkomst()">Delen</a> <a href="java script:KeerUitkomst()">Keer</a><BR/><a href="java script:MinUitkomst()">Min</a> <a href="java script:PlusUitkomst()">Plus</a>should be<a href="java script:Delen()">Delen</a> <a href="java script:Keer()">Keer</a><BR/> <a href="java script:Min()">Min</a> <a href="java script:Plus()">Plus</a> Link to comment Share on other sites More sharing options...
ShadowMage Posted June 14, 2010 Share Posted June 14, 2010 Always there to set me straight. You're lucky I'm not adverse to learning something new everyday... or else I might think it was something personal! And dsonesuk set us both straight. Neither of us pointed this out: function names compared to called function names<a href="java script:DelenUitkomst()">Delen</a> <a href="java script:KeerUitkomst()">Keer</a><BR/><a href="java script:MinUitkomst()">Min</a> <a href="java script:PlusUitkomst()">Plus</a>should be<a href="java script:Delen()">Delen</a> <a href="java script:Keer()">Keer</a><BR/> <a href="java script:Min()">Min</a> <a href="java script:Plus()">Plus</a> Link to comment Share on other sites More sharing options...
thescientist Posted June 14, 2010 Share Posted June 14, 2010 oh yeah...duh. Link to comment Share on other sites More sharing options...
MrDNA Posted June 15, 2010 Author Share Posted June 15, 2010 Bluh thats quite obvious... Thank you. Dont know how I could have missed that.. Good thing im still learning.Thanks again guys! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.