Well, I'm doing a little code in JS , in it I need to save the initial values of my html . I have a paragraph field ( <p>
) that starts in a way, I wanted to save the initial state of this paragraph so I always compare the current state with the initial state, but for some reason this is not working.
The HTML code line:
<p class="cont" id="res">A soma dos primeiros <input id="cont" type="number" name="num" min="0" max="100000000"> multiplos de <input id="mul1" type="number" name="num" min="1" max="1000"> e <input id="mul2" type="number" name="num" min="1" max="1000"> é: </p>
JS Code:
window.onload = function() {
//quando a janela carrega eu salvo o estado inicial do <p>
var pad = document.getElementById("res");
//funcao q faz o calculo
document.getElementById("calcmult").onclick = function () {
var contador = document.getElementById("cont").value;
var num1 = document.getElementById("mul1").value;
var num2 = document.getElementById("mul2").value;
var i=0;
var x=0;
var y=0;
while(i<=contador){
if(x%num1 == 0 && x%num2 == 0){
y = y + x;
i++;
}
x++;
}
//defino que x é igual ao paragrafo
x = document.getElementById("res");
//o html de x é igual ao valor inicial do paragrafo + y que é o valor
x.innerHTML = pad.innerHTML + y;
//zero os valores que estavam nos campos
document.getElementById("cont").value = '';
document.getElementById("mul1").value = '';
document.getElementById("mul2").value = '';
}
}
For some reason it saves the initial state all the time, causing the initial state to change and not doing the effect I want. Now when I compute it, in the second run, it concatenates the response values rather than simply compare with the initial state, which still has no answer, and give only one value.
My final goal is, for example, I have the following paragraph:
Type the first number to add the value