Well, basically what I understand is that you want to capture the changes of each element of the class ocultosOuOpcionais
and then add them up:
Capturing the difference in characters
As explained in that question the best way to calculate the difference between two strings is using the algorithm of Distance from Levenshtein:
function levenshtein(str1, str2) {
var m = str1.length,
n = str2.length,
d = [],
i, j;
if (!m) return n;
if (!n) return m;
for (i = 0; i <= m; i++) d[i] = [i];
for (j = 0; j <= n; j++) d[0][j] = j;
for (j = 1; j <= n; j++) {
for (i = 1; i <= m; i++) {
if (str1[i-1] == str2[j-1]) d[i][j] = d[i-1][j-1];
else d[i][j] = Math.min(d[i-1][j], d[i][j-1], d[i-1][j-1])+1;
}
}
return d[m][n];
}
In my example the function is named captureChanges()
.
To capture this difference from the initial and current value of a input
, use the .defaultValue
and the .value
, respectively. I adapted to your code in the following way:
var ocultosOuOpcionais = [].slice.call(document.querySelectorAll(".ocultosOuOpcionais"));
var mudados = [];
var mudancas = [];
var ocultosOuOpcionaisMudados = 0;
ocultosOuOpcionais.forEach(function (input, indice) {
input.addEventListener("input", function (event) {
if (event.target.defaultValue == event.target.value) {
mudados[event.target.id] = 0;
} else {
mudados[event.target.id] = event.target.defaultValue;
}
mudancas[indice] = captureChanges(mudados[event.target.id], event.target.value);
});
The above code is an adaptation to the Tobymosque Jsfiddle present in the question comments.
Done this, just add up the differences of each input
:
input.addEventListener('keyup', function(){
mudancas.forEach(function(el, index){
ocultosOuOpcionaisMudados += parseFloat(el);
})
ocultosOuOpcionaisMudados = 0; // É importante que ao final se zere o valor para que não se acumule exacerbadamente
})
Putting all the code together
function captureChanges(str1, str2) {
var m = str1.length,
n = str2.length,
d = [],
i, j;
if (!m) return n;
if (!n) return m;
for (i = 0; i <= m; i++) d[i] = [i];
for (j = 0; j <= n; j++) d[0][j] = j;
for (j = 1; j <= n; j++) {
for (i = 1; i <= m; i++) {
if (str1[i - 1] == str2[j - 1]) d[i][j] = d[i - 1][j - 1];
else d[i][j] = Math.min(d[i - 1][j], d[i][j - 1], d[i - 1][j - 1]) + 1;
}
}
return d[m][n];
}
var ocultosOuOpcionais = [].slice.call(document.querySelectorAll(".ocultosOuOpcionais"));
var mudados = [];
var mudancas = [];
var n = 0;
var ocultosOuOpcionaisMudados = 0;
ocultosOuOpcionais.forEach(function(input, indice) {
input.addEventListener("input", function(event) {
if (event.target.defaultValue == event.target.value) {
mudados[event.target.id] = 0;
} else {
mudados[event.target.id] = event.target.defaultValue;
}
mudancas[indice] = captureChanges(mudados[event.target.id], event.target.value);
});
input.addEventListener('keyup', function() {
mudancas.forEach(function(el, index) {
n += parseFloat(el);
})
ocultosOuOpcionaisMudados = n;
n = 0;
document.getElementsByTagName("h4")[0].innerHTML = "Mudanças no total:";
document.getElementsByTagName("p")[0].innerHTML = ocultosOuOpcionaisMudados;
})
});
<input type="text" class="ocultosOuOpcionais">
<br>
<input type="text" class="ocultosOuOpcionais">
<br>
<input type="text" class="ocultosOuOpcionais">
<br>
<input type="text" class="ocultosOuOpcionais">
<br>
<h4></h4>
<p></p>
Cool, that’s right, but in a more elegant way! + 1
– gustavox
And now hehe how do I include this in the other question you answered? -) Like, some fields from that other question are with
display=none
, So when you change a select for example, you open a field that’s not accounted for... although I don’t know, in your other question code that’s already solved? Because for example, it can have 10 fields, but only 8 visible, and if you do not choose a certain option, these two will not open. In this case the bar will not finish completing before? I could understand?– gustavox
If the element is only hidden with CSS, it exists in the document and will be counted. It would not count if the inputs were created dynamically
– Renan Gomes