-1
I am facing problems taking input values, storing in variables, and then passing these variables to an array of objects. I would also like to later display these values within each separate div and at the end add all div in one div only.
What should I do? Can you help me?
var dia = document.querySelector('#datas').value; //input
var conteudo = document.querySelector('#descricao').value; //input
var valores = document.querySelector('#valor').value; //input
var btn = document.querySelector('#btn');
var osGastos = document.querySelector('#osGastos'); //div
var gastos = {
data:"",
descricao:"",
valor:"",
set dat(value) {
this.data = value;
},
set descri(value) {
this.descricao = value;
},
set vall(value) {
this.valor = value;
}
};
function exibeGastos() {
for (var i = 0; i < gastos.data.length && gastos.descricao.length && gastos.valor.length; i++) {
var divElement = document.createElement('div');
var divElement1 = document.createElement('div');
var divElement2 = document.createElement('div');
var divText = document.createTextNode(gastos.data[i]);
var divText1 = document.createTextNode(gastos.descricao[i]);
var divText2 = document.createTextNode(gastos.valor[i]);
divElement.appendChild(divText);
divElement1.appendChild(divText1);
divElement2.appendChild(divText2);
osGastos.appendChild(divElement);
osGastos.appendChild(divElement1);
osGastos.appendChild(divElement2);
}
}
exibeGastos();
function addGasto() {
var dia = document.querySelector('#datas').value;
var conteudo = document.querySelector('#descricao').value;
var valores = document.querySelector('#valor').value;
gastos.dat = dia;
gastos.descri = conteudo;
gastos.vall = valores;
dia.value = '';
conteudo.value = '';
valores.value = '';
exibeGastos();
}
btn.onclick = addGasto;
function moeda(a, e, r, t) {
let n = ""
, h = j = 0
, u = tamanho2 = 0
, l = ajd2 = ""
, o = window.Event ? t.which : t.keyCode;
if (13 == o || 8 == o)
return !0;
if (n = String.fromCharCode(o),
-1 == "0123456789".indexOf(n))
return !1;
for (u = a.value.length,
h = 0; h < u && ("0" == a.value.charAt(h) || a.value.charAt(h) == r); h++)
;
for (l = ""; h < u; h++)
-1 != "0123456789".indexOf(a.value.charAt(h)) && (l += a.value.charAt(h));
if (l += n,
0 == (u = l.length) && (a.value = ""),
1 == u && (a.value = "0" + r + "0" + l),
2 == u && (a.value = "0" + r + l),
u > 2) {
for (ajd2 = "",
j = 0,
h = u - 3; h >= 0; h--)
3 == j && (ajd2 += e,
j = 0),
ajd2 += l.charAt(h),
j++;
for (a.value = "",
tamanho2 = ajd2.length,
h = tamanho2 - 1; h >= 0; h--)
a.value += ajd2.charAt(h);
a.value += r + l.substr(u - 2, u)
}
return !1
}
<main>
<p style="position:fixed; background-color: white; width: 100%; margin-top: -20px; font-size: 1.2em;">Despesas</p>
<section id="osGastos">
</section>
</main>
<footer>
<input type="number" id="datas" placeholder="DIA" min="1" max="31">
<input type="text" id="descricao" placeholder="DESCRIÇÃO">
<input type="text" id="valor" placeholder="VALOR" onKeyPress="return(moeda(this,'.',',',event))">
<button id="btn">OK</button>
</footer>
<script src="js/main.js"></script>
managed to solve your problem?
– Leandro Angelo