Basic input system for a few form fields I am attempting to port a linear function that I designed in a spreadsheet into HTML and Javascript.  It's based on linear least squares where I have to take a Matrix and multiply it by its transformation. At any rate. I am attempting to lay out a basic input system in Javascript/HTML where I can enter in variable data.  I have to take in quite a few more inputs, so I am looking for a more maintainable code pattern with less duplication.

In the name of make it work, then make it better. I came up with the following code.

Very new to javascript and the scope is hurting my brain! I am thinking that a dictionary (or similar) where I can pass that to a function then iterate over the values to update my HTML view. But again I am really new to Javascript. 

The code also in JS BIN.



 

window.onload = function() {

    var EnameField = document.getElementById('EnameField');
    var ENameValue = undefined;
    var MnameField = document.getElementById('MnameField');
    var MNameValue = undefined;
    var CnameField = document.getElementById('CnameField');
    var CNameValue = undefined;
    var FnameField = document.getElementById('FnameField');
    var FNameValue = undefined;

    updateNameDisplay();

    setInterval(updateNameDisplay, 100);

    function updateNameDisplay() {
        var thisEValue = EnameField.value || " ";
        if (ENameValue != thisEValue) {
            document.getElementById('EnameDisplay').innerHTML = ENameValue = thisEValue;
        }


        var thisMValue = MnameField.value || " ";
        if (MNameValue != thisMValue) {
            document.getElementById('MnameDisplay').innerHTML = MNameValue = thisMValue;
        }


        var thisCValue = CnameField.value || " ";
        if (CNameValue != thisCValue) {
            document.getElementById('CnameDisplay').innerHTML = CNameValue = thisCValue;
        }


        var thisFValue = FnameField.value || " ";
        if (FNameValue != thisFValue) {
            document.getElementById('FnameDisplay').innerHTML = FNameValue = thisFValue;
        }

    }

};
&lt;!DOCTYPE html&gt;
&lt;html&gt;
   &lt;head&gt;
      &lt;meta charset=utf-8 /&gt;
      &lt;title&gt;Test Page&lt;/title&gt;
   &lt;/head&gt;
   &lt;body&gt;
      &lt;form&gt;
         How many E's &lt;input type='text' id='EnameField'&gt;&lt;br&gt;
         How many M's &lt;input type='text' id='MnameField'&gt;&lt;br&gt;
         How many C's &lt;input type='text' id='CnameField'&gt;&lt;br&gt;
         How many F's &lt;input type='text' id='FnameField'&gt;&lt;br&gt;
      &lt;/form&gt;
      &lt;hr&gt;
      E's: &lt;span id='EnameDisplay'&gt;??&lt;/span&gt;&lt;br&gt;
      M's:&lt;span id='MnameDisplay'&gt;??&lt;/span&gt;&lt;br&gt;
      C's: &lt;span id='CnameDisplay'&gt;??&lt;/span&gt;&lt;br&gt;
      F's: &lt;span id='FnameDisplay'&gt;??&lt;/span&gt;&lt;br&gt;
   &lt;/body&gt;
Handling the Problem

To make this scalable you need to generate the HTML to fit however many items you want to include. To Render it this way you need a data structure to hold what you want to render. 

First Think of the data structure then what functions you need to render this data.

My Solution

I picked the following data structure.

[{
  title: String
  value: Number
}]


Here is the list of render functions I picked.


renderForm -> Render just the form
renderResults -> Render just the results
render -> Render the entire screen


And finally, we will need to capture the data changes from the form to update the data structure and then call renderResults so I created the onFormChange function.

Code



// Code goes here

const data = [{
  title: 'E',
  value: 0
}, {
  title: 'M',
  value: 0
}, {
  title: 'C',
  value: 0
}, {
  title: 'F',
  value: 0
}]

function onFormChange (value, i) {
  data[i].value = value;
  renderResults();
}

function renderForm () {
  const elForm = document.getElementById('form');
  let html = '';
  elForm.innerHTML = '';
  data.forEach((d, i) =&gt; {
    html += `
      How many ${d.title}'s 
      &lt;input type='text' value=${d.value} onchange="onFormChange( this.value, ${i})"&gt;
      &lt;br&gt;
    `;
  })
  elForm.innerHTML = html;
}
function  renderResults () {
  const elResults = document.getElementById('results');
  let html = '';
  elResults.innerHTML = '';
  // Render Each Element
  data.forEach((d, i) =&gt; {
    html += `
      ${d.title}: ${d.value}&lt;br&gt;
    `;
  })
  // Render Total
  let sum = 0;
  data.forEach((d) =&gt; {
   sum += Number(d.value) || 0
  })
  html += `
    &lt;div&gt;Total is ${sum}&lt;/div&gt;
  `
  elResults.innerHTML = html;
}

function render () {
  renderForm()
  renderResults()
}

window.onload = render;
  
&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;form id="form"&gt;
    &lt;/form&gt;
    &lt;hr&gt;
    &lt;div id="results"&gt;
    &lt;/div&gt;
  &lt;/body&gt;

&lt;/html&gt;




Final Thoughts

I did add a total div at the end of the results to show how you can also do some calculations on the entire data set when any of the values change.

Go ahead and add any additional data to the data array and have fun with it.