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>
Samsung Galaxy Watch8 is now selling at its lowest price ever by Fiza Ali
Amazon is now offering Samsung Galaxy Watch8 at its lowest price yet with a 34% discount (purchase link down below). The Galaxy Watch8 is equipped with a 1.5-inch Super AMOLED display with a resolution of 480x480 pixels and support for 16 million colours.
The watch is powered by a penta-core processor with clock speeds of up to 1.6GHz, runs Wear OS, and includes 2GB of RAM and 32GB of internal storage. For connectivity, the watch supports Bluetooth 5.3, Wi-Fi 802.11a/b/g/n on both 2.4GHz and 5GHz bands, and NFC. Furthermore, supported Bluetooth profiles include A2DP, AVRCP, HFP, and HSP. Location services are provided through GPS, GLONASS, BeiDou, and Galileo satellite systems.
Moreover, the Galaxy Watch8 includes a range of sensors as well, including an accelerometer, barometer, bioelectrical impedance analysis (BIA) sensor, electrical heart sensor (ECG), optical heart rate sensor, gyroscope, geomagnetic sensor, infrared temperature sensor, and ambient light sensor.
For media playback, the watch supports MP3, M4A, 3GA, AAC, OGG, OGA, WAV, AMR, and AWB audio formats. In terms of water resistance, it has a 5 ATM rating, which should make it suitable for swimming and everyday exposure to water. Finally, the device is powered by a 435mAh lithium-ion battery, and when it comes to its performance, Samsung rates the battery for up to 40 hours of use with the Always-On Display turned off.
Samsung Galaxy Watch 8 (2025) 44mm Smartwatch: $249.99 (Amazon US) - 34% off
Good to know
This Amazon deal is U.S. specific, and not available in other regions unless specified.
We only use first-party seller links (at the time of article publishing); ensure that you purchase from a first-party seller link only.
Check out Today's Deals on Amazon | or our recent tech deals.
Become a Prime member (for Students or SNAP) via Neowin
Get Prime Access - Prime for half price (for qualifying Medicaid, EBT, SNAP)
Subscribe to Prime Video, Audible Plus, Music Unlimited or Kindle Unlimited via Neowin
As an Amazon Associate, we earn from qualifying purchases.
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