0
Hey, guys. What’s up? I just started studying Javascript and, to train, I figured out a simple exercise: A screen, where there is the "Add Product" button that adds a row in the table below. It’s kind of like an e-commerce shopping cart. My question is this: When adding a line, I wanted the "+" button to add a unit in the amount and the "-" to take out. The problem is that for the first item, I even managed to program, but for those that will be generated after "Add Product" I could not at all. I cleaned up the code, because because of the various tests I had done, it was a mess. Then, I really wanted this help. How can I make the add and remove buttons to be created already programmed to mess with the amount of your line?
// Write JavaScript here
window.onload = function() {
tableEl = document.getElementById('tabelaDados');
btnAddLine = document.getElementById('btnAddLine');
btnAddLine.addEventListener('click', function(){
var tr = document.createElement('tr');
tr.innerHTML =
`
<td><button>-</button> 1 <button>+</button> </td>
<td>carro</td>
<td>R$ 55.000,00</td>
<td>R$ 55.000,00</td>
`
tableEl.appendChild(tr);
});
}
#tabelaHeader {
height: 50px;
width: 600px;
}
#tabelaDados {
position: relative;
height: 50;
width: 600px;
left: 60px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Usuários - Teste </title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" type="text/css" href="../css/estilo.css">
</head>
<body>
<div class="menu">
<button id="btnAddLine">Adicionar Produto</button>
</div>
<table id="tabelaHeader">
<thead>
<tr>
<th>Quantidade</th>
<th>Descrição</th>
<th>Valor Unitário</th>
<th>Valor Final</th>
</tr>
</thead>
</table>
<table id="tabelaDados">
<thead>
<tr>
<td><button>-</button>1 <button>+</button> </td>
<td>carro</td>
<td>R$ 55.000,00</td>
<td>R$ 55.000,00</td>
</tr>
</thead>
</table>
If you could take a look to help me, I’d appreciate it!
thank you so much for your help! It was extremely simple, as you said yourself. Can you just explain something to me, just so I understand? No jQuery, lines: var $tr = $(this). Parent(). Parent() var $Qtd = $tr.find('.Qtd') $Qtd.text(parseint($Qtd.text())+1) What it does is: - Pick up the 'tr' that parent (which is in innerHTML) - search for the span Qtd - apply the addition of the quantity The idea is this then, right? the "this" that is in the $tr variable refers to the #tableDados? Thank you again, Neuber! :)
– Gus
this do tr is pointing to the clicked element, . sub and .add. As for Parent(). Parent() and that’s right, I go to tr and Partit dai and look for Qtd, in fact only needed a Parent(). I’m not going to say it’s the right way, it’s just how I do it, because depending on how you change the structure of HTML you’ll need to change the js,.....
– Neuber Oliveira
Perfect, Neuber! Thank you so much for your help, explanation and patience, really! :)
– Gus
if the answer helped you choose it as the best, so Voce also contributes to the site ^^
– Neuber Oliveira