I would like to create a simple interface to allow a user to make a few choices via select boxes and drive a resulting equation.
Here's a screenshot:
This is a gross simplification and I am looking for guidance/suggestions throughout my existing code.
Some areas of concern:
- Should I be looping through the inputs?
- When/how should I be calling the
update()
function? - Where is the proper place to put my
scripts
-- that ishead
vs.body
vs. external.js
file? - Is using
onchange
a good approach? Is there a better approach? - Should I be assigning the values, e.g.
A = 1
,B = 2
, andC = 3
in theHTML
option
attributes or is this better handled in the script?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Using Select Boxes to Drive Equation Output</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script type="text/javascript" async src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Awesome Model to Predict Cool Stuff</h1>
<p>This is a test. Make some selections below.</p>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-3">
<label for="sel1">Input #1</label>
<select class="form-control" id="sel1" onchange="update()">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</div>
<div class="col-md-3">
<label for="sel2">Input #2</label>
<select class="form-control" id="sel2" onchange="update()">
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
</div>
<div class="col-md-3">
<label for="sel3">Input #3</label>
<select class="form-control" id="sel3" onchange="update()">
<option value=1>A</option>
<option value=2>B</option>
<option value=3>C</option>
</select>
</div>
<div class="col-md-3">
<label for="sel2">Input #4</label>
<select class="form-control" id="sel4" onchange="update()">
<option>5</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</div>
</div> <!-- End select input rows -->
<div class="row">
<div class="col-md-12">
<br>
<p class="text-center"><em>Note the underlying super complicated modeling equation is ` = 2 * Input_1 + 3 * Input_2 + 5 * Input_3 + 1.2 * Input_4` where `A = 1, B = 2,` and `C = 3`</em></p>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h4 class="text-center">After putting this through the model, your results are...</h4>
<div id="result" class="alert alert-info text-center"></div>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script>
var input_1 = $('#sel1');
var input_2 = $('#sel2');
var input_3 = $('#sel3');
var input_4 = $('#sel4');
update();
function update() {
var result = 2 * input_1.val() + 3 * input_2.val() + 5 * input_3.val() + 1.2 * input_4.val();
$('#result').text(result + " widgets");
}
</script>
</body>
</html>