John Hebert Trindade Posted January 21, 2024 at 09:53 PM Report Share #632653 Posted January 21, 2024 at 09:53 PM Boa noite a todos, tenho um formulário como se fosse um carrinho de compras. no form tenho a tabela da seguinte forma: <input type="button" name="addProduto" id="addProduto" class="input-adm btn-warning" value="Incluir"> <table class="table-list"> <thead class="list-head"> <tr> <th class="list-head-content sizeone table-md-none">Código Barras</th> <th class="list-head-content sizefive table-md-none">ID</th> <th class="list-head-content sizetwo">Nome do produto</th> <th class="list-head-content sizethree">Quantidade</th> <th class="list-head-content sizefour">Ações</th> </tr> </thead> <tbody class="list-body"> </tbody> </table> Em javascript adiciono os produtos a tabela, o adicionar está funcionando, mas o remover não funciona, o que errei? //Adicionar Produtos ao carrinho document.getElementById('addProduto').addEventListener('click', incluirProduto) //Remover Produtos ao carrinho document.getElementById('.remove').addEventListener('click', removerProduto) function incluirProduto(){ const selectProduct = document.getElementById('adms_produto_id') const optionText = selectProduct.options[selectProduct.selectedIndex].text const id = selectProduct.options[selectProduct.selectedIndex].value const barcode = selectProduct.options[selectProduct.selectedIndex].getAttribute('data-barcode') const qtde = document.getElementById('qtde').value const html = `<tr><td class="list-body-content table-md-none sizeone"><input type="text" name="codigo_barras[]" id="codigo_barras" class="input-adm" placeholder="Código Barras" autocomplete="on" required value="${barcode}"></td><td class="list-body-content table-md-none sizefive" name="id[]">${id}</td><td class="list-body-content sizetwo">${optionText}</td><td class="list-body-content sizethree"><input type="number" name="qtde[]" id="qtde" class="input-adm" placeholder="Qtde" autocomplete="off" required value="${qtde}"></td><td><input type="button" class="input-adm btn-danger" name="remove" id="remove" value="Remover"></td></tr>`; const x = 1; document.querySelector('tbody.list-body').innerHTML += html } function removerProduto(){ document.querySelector('tbody.list-body').closest('tr').remove(); } Link to comment Share on other sites More sharing options...
Solution Ivo Vicente Posted January 21, 2024 at 09:56 PM Solution Report Share #632655 Posted January 21, 2024 at 09:56 PM O document.getElementById('.remove').addEventListener('click', removerProduto) está à escuta de uma classe .remove que não existe no botão de remover Feito é melhor que perfeito Link to comment Share on other sites More sharing options...
John Hebert Trindade Posted January 21, 2024 at 10:04 PM Author Report Share #632656 Posted January 21, 2024 at 10:04 PM testei das seguinte formas e não resultou: const html = `<tr><td class="list-body-content table-md-none sizeone"><input type="text" name="codigo_barras[]" id="codigo_barras" class="input-adm" placeholder="Código Barras" autocomplete="on" required value="${barcode}"></td><td class="list-body-content table-md-none sizefive" name="id[]">${id}</td><td class="list-body-content sizetwo">${optionText}</td><td class="list-body-content sizethree"><input type="number" name="qtde[]" id="qtde" class="input-adm" placeholder="Qtde" autocomplete="off" required value="${qtde}"></td><td><input type="button" class="input-adm btn-danger remove" name="remove" id="remove" value="Remover"></td></tr>`; e assim: const html = `<tr><td class="list-body-content table-md-none sizeone"><input type="text" name="codigo_barras[]" id="codigo_barras" class="input-adm" placeholder="Código Barras" autocomplete="on" required value="${barcode}"></td><td class="list-body-content table-md-none sizefive" name="id[]">${id}</td><td class="list-body-content sizetwo">${optionText}</td><td class="list-body-content sizethree"><input type="number" name="qtde[]" id="qtde" class="input-adm" placeholder="Qtde" autocomplete="off" required value="${qtde}"></td><td><input type="button" class="remove" name="remove" id="remove" value="Remover"></td></tr>`; Link to comment Share on other sites More sharing options...
John Hebert Trindade Posted January 21, 2024 at 11:11 PM Author Report Share #632657 Posted January 21, 2024 at 11:11 PM Já consegui resolver. Se coloco como um input não funciona, mas se coloco como button já da certo Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now