• 0

onChange and Calculating Fields


Question

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

  • 0

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?

Your problem might be that onchange only fires when one has finished editing a field. It's similar to onblur, except it fires only if contents has been changed. It isn't exactly instant, whatsoever.

w3schools only confused me more with their explanation.

By somewhat popular convention, W3Schools is a resource frowned upon. It's also not related to W3C at all. It may be ok to use when one's just forgetting things but not when learning for the first time.

Might want to see http://www.neowin.ne...06-xhtmlcssxml/ , where a lot helpful Neowinians each have listed their most useful website building help & resources.

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?

Use onkeyup event. That one will fire on everything except for cutting, copying and pasting using a mouse (where onmouseup doesn't help either).

Like this:

<input class="right" type=text name="inputBox1" value="" onkeyup="calcMultiply(); calcSquareRoot();"><BR>

Or more to my XHTML Strict liking (see recommendations below):

<input type="text" name="inputBox1" value="" style="text-align: right" onkeyup="calcMultiply(); calcSquareRoot();" /><br />

I would appreciate any feedback on my mistakes.

Your markup and code is full of all kinds of errors. It works only because browsers are exceptionally forgiving (a feature that backfires horribly when you least expect it). Of course, it doesn't matter much if this is just a flyby course and you're not about to use it ever again. In all other cases I'd suggest visiting the thread linked above, check basic things like standards and their related doctypes and finally grind your pages through the W3C Validator (http://validator.w3.org/). It might seem like a major case of OCD - why bother if it works as it is - but it does help a lot when it suddenly doesn't.

  • 0

Thank you cralias for taking the time to help me out! I am still unsuccessful in my attempt to have all three fields calculated when a number is entered into any one of them. But it will be something I'll revisit later and hopefully be able to get working once I understand Javascript better.

I picked up the book Javascript: A Beginner's Guide to help me with the fundamentals and to get a decent foundation for the language. Scripting/coding in general is something I have little interest in and continually struggle with, but since I'm taking this class which covers Javascript, I want to have a solid understanding of it.

Again I want to thank you cralias for your assistance and advice. Especially the w3 validator, as I was able to clean up my code quite a bit after running it through the validator a couple times. Here's what my script looks like now in case anyone is curious. It doesn't meet all of the requirements for my assignment but it close enough to being acceptable.

<!DOCTYPE HTML>
<HTML>
  <HEAD>
  <SCRIPT TYPE="text/javascript">
// 1st box, 2nd box value / 5 and 3rd box value * 3rd box value
function calcBoxOne() {
one = document.myForm.inputBox2.value;
document.myForm.inputbox1.value = (two / 5 || three * three);
}

// 2nd box is 5 times the value of the number in box 1
function calcMultiply() {
two = document.myForm.inputBox1.value;
document.myForm.inputBox2.value = (one * 5 || three * three);
}

// 3rd box that equals the square root of number in box 1
function calcSquareRoot(){
one = document.myForm.inputBox1.value;
document.myForm.inputBox3.value = Math.sqrt(one);
three = document.myForm.inputBox3.value;
}
  </SCRIPT>

  <TITLE>
  Assignment 4
  </TITLE>
  </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="" onChange="calcBoxOne();" onFocus="calcBoxOne();"><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="" onChange="calcMultiply();" onFocus="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="" onChange="calcSquareRoot();" onFocus="calcSquareRoot();"><BR>
</FORM>
</DIV>
</BODY>
</HTML>

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Oh man, the memories.
    • Affinity by Canva 3.2.2.4557 by Razvan Serea Affinity by Canva is a free, all-in-one creative app combining vector design, photo editing, and page layout in a single platform. Originally developed by Serif, it’s now under Canva, offering professional-grade tools without subscriptions. Users get full access to Pixel, Vector, and Layout studios, plus ongoing updates. Designed for Mac and Windows, it empowers designers, illustrators, and content creators to work faster, smarter, and more creatively than ever before. Affinity is a unified, high-performance design platform combining vector, raster, and layout workflows. It offers fully non-destructive editing, advanced curve and shape manipulation, artboards, symbols, and seamless integration of pixel and vector content. The photo engine supports RAW editing, compositing, retouching, and batch processing with macro automation. Layout tools include long-form document support, typographic precision, navigational elements, CMYK-ready print, and Data Merge. Canva AI Studio adds generative tools (Fill, Expand, Edit), Depth Map, Super Resolution, and advanced portrait effects, accessible via Canva Pro or higher. Broad file compatibility and customizable workspaces ensure professional-grade efficiency. Advanced AI features like Generative Fill and Expand are unlocked in Affinity through the Canva AI Studio for users with a Canva premium plan (Pro, Business, Enterprise, or Education). Affinity is truly free. Every tool in the Pixel, Vector, and Layout studios is fully accessible, along with all customization and export options—no limits, no payments required. The app also receives free updates with new features and improvements. Your creativity remains yours. Affinity stores all your work locally on your device. Canva does not use any Affinity content to train AI or develop features, including anything created with Canva AI tools within Affinity. If you export or upload your work to Canva, you remain in full control. Data preferences can be reviewed or updated at any time in your Canva account settings. Why is Affinity free? Curious how this is possible? Here’s the philosophy behind it and how it works. Note: A free Canva account is required to use Affinity. Your account gives access to Affinity along with other Canva products and features. Download: Affinity 3.2.2.4557 | ARM64 | ~600.0 MB (Freeware) Links: Affinity Website | macOS | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Finally we can whole-heartedly recommend the AMD 9070 GRE thanks to this discount by Sayan Sen Earlier this month at Computex 2026 AMD released the Radeon RX 9070 GRE for a global audience. We reviewed the GPU and it scored 8 out of 10 for gaming and 7.5 out of 10 for productivity including things like AI performance. The card launched for $550 which we thought was a bit on the higher side, though currently you are getting the chance to get the Gigabyte Gaming variant of the GPU for just $500 thanks to this discount (purchase link under the specs table down below). As such we can finally whole-heartedly recommend this GPU. The card employs Gigabyte's WINDFORCE cooling system which combines multiple design elements such as alternate-spinning Hawk fans, a vapor chamber, and composite heat pipes to manage heat dissipation. The Server-grade Thermal conductive gel further helps in this department. The inclusion of a semi-passive cooling mode allows the fans to remain inactive under low workloads, which allows for reduced noise operation during lighter usage. There is dual BIOS switch that allows toggling between performance and silent modes. The technical specs of the Gigabyte Gaming RX 9070 GRE OC is given in the table below: Specification Value Boost Clock Up to 2920 MHz Game Clock Up to 2340 MHz Stream Processors 3072 Memory Clock 18 Gbps Memory Size 12 GB Memory Type GDDR6 Memory Bus 192-bit Memory Bandwidth 432 GB/s Card Bus Interface PCI Express 5.0 Max Resolution 7680 × 4320 Max Displays (Multi-view) 4 Card Dimensions 288 × 132 × 50 mm (L × W × H) Recommended Power Supply 700 W Power Connectors 2 × 8-pin Display Outputs 2 × DisplayPort 2.1a 2 × HDMI 2.1b Get it at the link below: GIGABYTE Gaming Radeon RX 9070 GRE 12GB GDDR6 GPU (GV-R907GREGAMING OC-12GD): $549.99 + $50 discount with promo code in mail => $499.99 (Sold and Shipped by Newegg US) This Newegg deal is US-specific and not available in other regions unless specified. This is a first-party seller link (at the time of article publishing); ensure that you also purchase from a first-party seller link only. If you don't like it or want to look at more options, check out the previous deals that we have covered, OR you can also visit Amazon US deals page. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an affiliate partner, we earn from qualifying purchases.
  • Recent Achievements

    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      513
    2. 2
      +Edouard
      205
    3. 3
      PsYcHoKiLLa
      136
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      85
  • Tell a friend

    Love Neowin? Tell a friend!