Solved

Automatically adjust numbers in input fields to match max value

Posted on 2015-02-04
4
63 Views
Last Modified: 2015-02-08
Hi,

I got a very specific and interesting task, but i'm not sure where to begin, but very excited to get started :)
I have been working on an online Cylinder Configurator for the company I work in and everything has been working fine so far, but the next part is a bit tricky and i'm not sure where to start.

Please see attached PDF sketch.

- I need to place 4 input fields on top of an image-background, this is no problem.

Here is my problem:
- If "A" is changed, then "D" need to be adjusted accordingly.
- If "B" is changed, then "D" need to be adjusted accordingly.
- If "C" is changed, then "D" need to be adjusted accordingly.
- If "D" is changed, then "A" need to be adjusted accordingly.

Any idea how this is normally done? I would prefer to use some kind of "live updating" solution, without a "page load".


Regards,

Kenneth
input-fields.pdf
0
Comment
Question by:kgp43
  • 2
4 Comments
 
LVL 18

Expert Comment

by:nap0leon
ID: 40588864
Simplistic example of how to bind the recalculation to whenever any of the inputs change.
In the example I hard coded the new value for A and D.  Obviously, you'll want to use your real formula instead.  Also, this example does not do anything to ensure that the values in the input fields are numeric, you'll probably want to add that in too.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>IIS7</title>
	<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<script>
function recalcInputs(element) {
  if ($(element).attr('name') == 'd') {
    //math to recalculate a goes here
    newAvalue = 12;
    $('input[name="a"]').val(newAvalue);
  } else {
    //math to recalculate d goes here
    newDvalue = 77;
    $('input[name="d"]').val(newDvalue);
  }
}

</script>
<body>
A: <input name="a" value="" type="text" onblur="javascript:recalcInputs(this)"><br/>
B: <input name="b" value="" type="text" onblur="javascript:recalcInputs(this)"><br/>
C: <input name="c" value="" type="text" onblur="javascript:recalcInputs(this)"><br/>
D: <input name="d" value="" type="text" onblur="javascript:recalcInputs(this)"><br/>
</body>
</html>

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40590840
This is the perfect task for AngularJS!
0
 

Author Closing Comment

by:kgp43
ID: 40596715
Going to look into that, exactly what i'm looking for it seems.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40596876
I've been using AngularJS for about a month, still just stumbling along at this point.  It mixes the declarative notation of HTML with the imperative structure of a programming language.  And while I think it's a laudable effort to turn a silk purse into a sow's ear, the next "real" solution for online web applications is probably still out in the future, and is probably not back-level compatible with JavaScript and HTML.  That aside, AngularJS is a really big step in the right direction!

Thanks for the points and best of luck with your project! ~Ray
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now