• 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

    • Macbook Air is an appealing option, as are plethora of Windows devices with various different CPU's
    • Mozilla highlights Firefox Nova 2026 redesign and more upcoming features with new roadmap by Sayan Sen Last month Mozilla confirmed that Firefox was set to get a major redesign this year. Dubbed "Project Nova", it can already be tested and will roll out to all users later this year.The idea is to keep the browser competitive in a rapidly evolving internet landscape. As such the revamp focuses on improving privacy, usability, performance, accessibility, and customization. Key privacy features including the built-in VPN, private browsing mode, and Enhanced Tracking Protection, will be more visible and easier to manage, while users will have the option to disable AI features entirely through a dedicated kill switch. Additionally, the redesign promises faster page loading, the return of Compact mode, expanded personalization options, and stronger accessibility support. You can find the full details in the dedicated piece linked above. In a new blog post today the company once again reiterated on Nova and also emphasized other new and upcoming features like the settings revamp that is intended to make it easier for users to understand browser settings. In order to make it simpler for users to keep up with such features Mozilla today is launching Firefox roadmap. Hence enthusiasts and interested users will be able to check out what's cooking and also share feedback about the upcoming additions. Alongside the roadmap announcement, Mozilla also highlighted what's new in Firefox 152. One of the biggest additions is the arrival of Tab Groups on Android. The feature, which has already been helping desktop users organize large numbers of tabs, is now beginning to roll out on mobile. Users will be able to group related tabs together, assign names and colors to them, and return to them later. Mozilla says support for iOS will arrive later this year. Firefox 152 also introduces the aforementioned redesigned Settings experience. The company says the changes are meant to make controls easier to find and help users discover features they may not have previously known about. Existing preferences are not changing, though they are now better organized. Another notable addition is the new Blocked Tracker Widget, which provides a visual overview of Firefox's privacy protections by showing how many trackers have been blocked over time and the types of tracking activity the browser has stopped. Looking ahead, Mozilla revealed several upcoming roadmap features. They include customizable keyboard shortcuts, as well as enhanced PDF editing tools that will allow documents to be split, merged, and reorganized directly within Firefox. The company is also working on bringing Multi-Account Containers into the native Firefox experience thus removing the need for a separate extension. Meanwhile Firefox's built-in VPN is set to expand to mobile devices. Mozilla is also developing AI-powered features like Quick Answers, which can provide concise responses to voice queries, and Smart Window, its optional AI browsing experience that is now available without a waitlist. Finally, a new Power Saving Mode is in the works and will help reduce the impact of resource-heavy tabs on mobile devices in order to extend battery life. The video below summarizes the upcoming changes in an easy to understand format: You can find the announcement blog post here on Mozilla's official website.
    • Dead on arrival at that price. Like they missed the mark by multiple hundreds of dollars - this should actually undercut the Macbook Air at $899 if they want any sort of sales / further adoption of WoA
    • Wow, 50% increase for the base model. That's steep!
    • A group made up of dozens of cybersecurity experts, including several well-known veterans of the industry, published an open letter to the U.S. government asking it to lift the export control order on Anthropic’s Fable and Mythos models. According to the open letter, “this action has taken the best models away from [cybersecurity] defenders” who now can’t use the models to find vulnerabilities and make their software and products more secure. “To pull the best capabilities away from defenders without a good reason when our adversaries are rapidly advancing is dangerous,” read the letter. On Friday, the U.S. government ordered Anthropic to limit the export of Fable and Mythos, citing national security concerns, without explaining the specific reasons behind the order, according to Anthropic. In response, the company suspended access to the models to all users worldwide.     https://techcrunch.com/2026/06/15/cybersecurity-vets-protest-dangerous-us-government-ban-on-anthropics-most-powerful-models/
  • Recent Achievements

    • One Year In
      Console General earned a badge
      One Year In
    • One Year In
      Twozo Technologies earned a badge
      One Year In
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
    • Veteran
      branfont went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      511
    2. 2
      +Edouard
      200
    3. 3
      PsYcHoKiLLa
      109
    4. 4
      Steven P.
      89
    5. 5
      Nick H.
      71
  • Tell a friend

    Love Neowin? Tell a friend!