I am trying to write a html page with 3 fields that a user can choose to input a number into any of the three fields and the calculations are instantly performed and displayed.
The first field is a number. The second field is that first field multiplied by 5. The third field is the square root of the first field.
I've been tripped up on a couple issues as I'm new to javascript and have only taken a few programming classes in the past.
1) My first stumbling block, the onChange command. Couldn't get it working so I instead discovered and have used onFocus and onBlur. Anyone have a good tutorial on how to use onChange correctly? w3schools only confused me more with their explanation.
2) What approach should I use to allow a number to be entered into any of the three fields and have necessary calculations be displayed in all the fields?
3) I'm not asking for anyone to write this script out for me. I'm a beginner trying to complete this as an assignment for one of my classes. I'm looking for advice and guidance on how to make this work. What I've written so far is pasted below. The script works if a number is entered into the first box, but I need to be able to have a number entered into any of the boxes and have the calculations displayed instantly and I would appreciate any feedback on my mistakes.
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
// 2nd box is 5 times the value of the number in box 1
function calcMultiply() {
one = document.myForm.inputBox1.value;
document.myForm.inputBox2.value = (one * 5);
}
// 3rd box that equals the square root of number in box 1
function calcSquareRoot(){
document.myForm.inputBox3.value = Math.sqrt(one);
}
</SCRIPT>
</HEAD>
<BODY onLoad ="document.myForm.inputBox2.focus()">
<div style="width: 200px; text-align: center;">
<form name="myForm">
<!-- First box can have any number entered to be calculated -->
<P> A Number: </P>
<input class="right" type=text name="inputBox1" value=""><BR>
<!-- 2nd box must be 5 times the value of number in the first box -->
<P> Number Multiplied by 5: </P>
<input class="right" type=text name="inputBox2" value="" onFocus="calcMultiply();" onBlur="calcMultiply();"><BR>
<!-- 3rd box must be the square root of number in the first box -->
<P> Square Root of the Number: </P>
<input class="right" type=text name="inputBox3" value="" onFocus="calcSquareRoot();" onBlur="calcSquareRoot();"><BR>
</BODY>
</HTML>
I used to use Google assistant, not on the phone i have now, but about 7 years ago, then I decided it did not really do anything for me.
Because i had Echo units over the house I added Alexa to the phone to control stuff and that is how it is now. Not the new Alexa+, as that is not really available in the U.K yet apart from on new units and to be honest, not interested in it.
I went though the stage years ago of using voice to do text and call people, quicker to do it using my hands.
I had a muck about with Siri on my Mac when I first got it, but not having a microphone permanently plugged in makes it a pain. I know it can be used by text.
Siri like Apple AI is disabled on my Mac and will stay disabled.
I have a TV, but it is not used for normal linear TV, only streaming and it is not a Samsung and the best bit is, I don;'t and never have had a Instagram account. The only thing I have to do with Meta is Faceache and I only keep that just for the messaging part.
But building your own.. what? You can't build anything like the Steam Machine yourself. Even trying to get close costs a good deal more. Even just the CPU cooler in their price comparison is as big as the entire Steam Machine.
If you want a regular gaming PC, then by all means, build that. If you want a a small console-like PC for the living room that is good for gaming, I'm not sure what else is a better deal. In the GN review, they only mentioned a small form factor Dell, which is like twice the size and hundreds of dollars more expensive.
Question
Scotch Tape
I am trying to write a html page with 3 fields that a user can choose to input a number into any of the three fields and the calculations are instantly performed and displayed.
The first field is a number. The second field is that first field multiplied by 5. The third field is the square root of the first field.
I've been tripped up on a couple issues as I'm new to javascript and have only taken a few programming classes in the past.
1) My first stumbling block, the onChange command. Couldn't get it working so I instead discovered and have used onFocus and onBlur. Anyone have a good tutorial on how to use onChange correctly? w3schools only confused me more with their explanation.
2) What approach should I use to allow a number to be entered into any of the three fields and have necessary calculations be displayed in all the fields?
3) I'm not asking for anyone to write this script out for me. I'm a beginner trying to complete this as an assignment for one of my classes. I'm looking for advice and guidance on how to make this work. What I've written so far is pasted below. The script works if a number is entered into the first box, but I need to be able to have a number entered into any of the boxes and have the calculations displayed instantly and I would appreciate any feedback on my mistakes.
<HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> // 2nd box is 5 times the value of the number in box 1 function calcMultiply() { one = document.myForm.inputBox1.value; document.myForm.inputBox2.value = (one * 5); } // 3rd box that equals the square root of number in box 1 function calcSquareRoot(){ document.myForm.inputBox3.value = Math.sqrt(one); } </SCRIPT> </HEAD> <BODY onLoad ="document.myForm.inputBox2.focus()"> <div style="width: 200px; text-align: center;"> <form name="myForm"> <!-- First box can have any number entered to be calculated --> <P> A Number: </P> <input class="right" type=text name="inputBox1" value=""><BR> <!-- 2nd box must be 5 times the value of number in the first box --> <P> Number Multiplied by 5: </P> <input class="right" type=text name="inputBox2" value="" onFocus="calcMultiply();" onBlur="calcMultiply();"><BR> <!-- 3rd box must be the square root of number in the first box --> <P> Square Root of the Number: </P> <input class="right" type=text name="inputBox3" value="" onFocus="calcSquareRoot();" onBlur="calcSquareRoot();"><BR> </BODY> </HTML>Thank you.
Link to comment
https://www.neowin.net/forum/topic/1055910-onchange-and-calculating-fields/Share on other sites
2 answers to this question
Recommended Posts