I'm having a go at doing a simple little calculation with javascript like this: <SCRIPT> function addThreeNums (inOne, inTwo, inThree) { var inOne = Number(inOne); var inTwo = Number(inTwo); var inThree = Number(inThree); return Number(inOne / inThree * inTwo * '4.6' / '10'); } </SCRIPT> </HEAD> <BODY> <FORM Name="theForm" METHOD="post"> <INPUT Type=Text Name="num1"> <INPUT Type=Text Name="num2"> <INPUT Type=Text Name="num3"> <INPUT Type=Button Value="Calculate" onClick='alert("The sum of the three numbers is " + addThreeNums(theForm.num1.value,theForm.num2.value,theForm.num3.value));'> </FORM> Code (markup): and using 'alert' is the only way I know of getting the answer to display on the same page... so how do I get the result to display in the same page in another text box, or even a div?
For processing forms with javascript only, better use id instead of name, e.g: <input type="text" id="num1" />. And to show the result to a div in the document's body, you could paste the calculation result to div's innerHTML property, e.g: document.getElementById("divID").innerHTML="The sum of ...
Yes, as hdewantara said, you could print the result in the same page using a <div> tag. >>Declare an empty div tag in the body, with just its Id. >>Set the value of that tag from the JavaScript, like, var result = Number(inOne / inThree * inTwo * '4.6' / '10'); document.getElementById("divTagName").innerHTML = "The result is"+result; inside your function. HTH. -ishti.
<HTML><HEAD> <SCRIPT> window.onload = function() { var myForm = document.getElementById("theForm"); myForm.onsubmit = function() { var n1 = document.getElementById("num1").value; var n2 = document.getElementById("num2").value; var n3 = document.getElementById("num3").value; document.getElementById("result").innerHTML = n1 / n3 * n2 * 4.6 / 10; return false; } } </SCRIPT> </HEAD> <BODY> <FORM Id="theForm"> <INPUT Type="Text" Id="num1"> <INPUT Type="Text" Id="num2"> <INPUT Type="Text" Id="num3"> <INPUT Type="Submit" Value="Calculate"> <div id="result"></div> </FORM> </BODY> </HTML> Code (markup): I prefer not to use inline js (onclick='.....'), it looks much better when you don't, and apparently has other advantages.The window.onload = function() .... is there because the code is at the top of the page. If you didn't have this then errors would occur because the elements that are you referring to have not been loaded yet, you could remove this block if the code was at the bottom of the page because the elemets will have been loaded.
What does it mean when the little square beside rep recieved is grey, rather than green? I can't work it out and there is no explanation. At first I thought it was when someone marks 'I disapprove'... but the comments next to it don't confirm that. Does anyone know what the difference is between grey and green?
Did the one I just made come out gray? I got one where it didn't match what they said, I assumed they chose disapprove by mistake.
Nah yours was green, but I have had a few that say things like 'thanks for the help!', with a grey box... I really couldn't care less about rep, but I can't work out why it is grey sometimes... Although it is good to know when your advice has been used and appreciated... it is a bit annoying when you spend half an hour coding a solution only to never hear another word or post from the person that you have spent that time helping. Maybe it is grey when the user has less than a certain number of posts... that is the only reason that I can think of.
I do know the more rep point you have the more rep points you give to people, so maybe you're right? Anyway, I always give rep points and some response when people help me. I also help other people if I spot threads in here that I know the answer to.
I mainly just try and help people. I find that it is a good way to learn. My programming skills have improved a LOT since i started participating in these forums...Even if i don't know the answer to a problem i will research it and try and work out a solution. That way, if I ever need to do the same task.. I will know exactly how.