2

I have the following code which loops through a number of fields, which their ID prefix and suffix is the same, see code below. I am stuck trying to add the total values of all related fields and displaying it on textbox Id txtTOTAL20, txtTOTAL40 and txtTOTAL50. This is what I have so far (which doesn't work)

for (var i = 1; i < 14; i++) {
    x = i + 1
    var field1 = "txtF" + i + "_1"; 
    var field2 = "txtF" + i + "_2"; 
    var field3 = "txtF" + i + "_3"
    FIELD20 = FIELD20 + parseInt(field1.value);
    FIELD40 = FIELD40 + parseInt(field2.value);
    FIELD50 = FIELD50 + parseInt(field3.value);
    var text = document.getElementById('txtTOTAL20');text.value = FIELD20;
    var text = document.getElementById('txtTOTAL40');text.value = FIELD40;
    var text = document.getElementById('txtTOTAL50');text.value = FIELD50;
}

Any ideas?

2 Answers 2

1

this should work:

//init
var FIELD20 = 0;
var FIELD40 = 0;
var FIELD50 = 0;

for (var i = 1; i < 14; i++) {
    //get values
    var field1 = document.getElementById("txtF" + i + "_1"); 
    var field2 = document.getElementById("txtF" + i + "_2"); 
    var field3 = document.getElementById("txtF" + i + "_3");

    //increment
    FIELD20 += parseInt(field1.value);
    FIELD40 += parseInt(field2.value);
    FIELD50 += parseInt(field3.value);
}

//display totals
document.getElementById('txtTOTAL20').value = FIELD20;
document.getElementById('txtTOTAL40').value = FIELD40;
document.getElementById('txtTOTAL50').value = FIELD50;
Sign up to request clarification or add additional context in comments.

4 Comments

Sure, but you ought to explain what you fixed and why this works.
great!, this code works great, except the final value (FIELD20) is NaN instead of the real number. I assume the text value is not being converted correctly.
ok, searching on the internet I found this, which works. Instead of using parseInt use Math.floor : field20 += Math.floor(field1.value); i appreciate your help, kantholy.
explanation: * FIELD20/40/50 was not initialized causing problems with incrementing the value * "document.getElementById" was missing on field1/2/3
0

What you want to do is best done via 'data binding'. Check out this basic example here:

var input = document.querySelector('#a');
var output = document.querySelector('p');

function update(event) {
   output.innerHTML = input.value;
}
input.addEventListener('keydown', update);
input.addEventListener('keyup', update);

We listen for key up and key down events and then change our values. Fiddle for fun.

This can be ungainly if you have a large table because we have to bind every input to it's output. Not so with ES6! Instead we still add listeners to our inputs, but we bind them all to a single JS object. That's one event listener instead of dozens of others. We do this via Object.observe.

Check it out:

var input = document.querySelectorAll('input');
var output = document.querySelectorAll('td');

input = Array.prototype.slice.call(input);

/*
Here you model out your "table" as a JS object
so that we can listen for changes more easily.
*/
var table = {
    a: '',
    b: ''
};

input.forEach(function addListeners(input) {
    //Closured! 
    function update(event) {
        table[input.id] = input.value
    }
    input.addEventListener('keydown', update);
    input.addEventListener('keyup', update);
});

//You'd have to make some more clever function.
function alphaToCell(alpha) {
    if (alpha === 'a') return 0;
    if (alpha === 'b') return 1;
}

/*
The cool thing here is that instead of binding each input to it's output, we just bind every input to a single model, and then that model knows how to update all the outputs.
*/
Object.observe(table, function (changes) {
    changes.forEach(function (change) {
        var index = alphaToCell(change.name);
        output[index].innerHTML = table[change.name];
    })
});

Fiddle for Fun.

5 Comments

thanks for your code/ideas. Nope, I don't want to display the data on each increment. I have 3 columns with 14 fields in each column. Everytime a user changes any of the values on the fields, I want the javascript code to recalculate the total (which is at the bottom) for each column.
I appreciate the time you have taken to show me. The thing is that the code above by kantholy works like a charm, with just a small change as mentioned on my comments.
@user1135218, no problem as long as you're learning something. Check out my new answer though. It'll blow your mind.
your code sounds great, and will keep it in mind for future projects. I see/undestand what it does and where it could be used but it's a bit too complex for me at the moment, I am just a beginner!
Yeah, haha :D I realized after reading your question a couple times that I had gotten a bit excited about ES6 and wasn't really answering your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.