1
When you add name, age and quantity will count for index ,but when you click to rule out erases everything quantity stays = 0 , name and age = by "empty" right there, but when I add again, the quantity starts with 2 (index) but I want to start with 1
Why did you stay for 2 ?
tried various things here I don’t know what to do, what I do?
let a = []
function adicionar() {
var nome = document.getElementById("nome")
var idade = document.getElementById("idade")
var r = document.getElementById("res")
if (nome.value == "" && idade.value) {
alert("Por favor digite o seu nome e sua idade!")
}
if (nome.value.length && idade.value.length) {
a.push(nome)
r.innerHTML += ` <tr><th scope="row">${a.length}</th><td>${nome.value}</td><td>${idade.value}</td>`
}
}
function excluir() {
var n = [nome, idade]
v = "vazio"
z = []
z.slice(n)
document.getElementById("res").innerHTML = `<th scope="row">${z.length}</th><td>${v}</td><td>${v}</td>`
}
<div class="container">
<h1 class="text-center mt-3">
Agenda
</h1>
</div>
<div class="container">
<table class="table">
<thead>
<tr>
<th scope="col">Quantidade</th>
<th scope="col">Nome</th>
<th scope="col">Idade</th>
</tr>
</thead>
<tbody id="res">
</tbody>
</table>
</div>
<div class="container text-center">
<button class="btn btn-danger" type="button" onclick="excluir()">Excluir</button>
<button class="btn btn-warning" type="button" onclick="editar()">Editar</button>
</div>
<div class="container">
<div class="text-center">
<h1 class="text-center mt-2 mb-4">Adicione por aqui!</h1>
<input type="text" id="nome" name="nome" placeholder="NOME">
<input type="number" name="idade" id="idade" placeholder="IDADE">
<button class="btn btn-primary" type="submit" id="btn" onclick="adicionar()">Adicionar</button>
</div>
</div>
Got it, thank you so much for your education and tips!
– Julio S
Friend sorry to ask you again, I already did to add and delete right? now missing (edit) , has a function of this js or not and how does?
– Julio S
@Julios This is the case of ask another question, but anyway, there are several ways to do it, depending on what you need (for example, you would need to choose which one to edit, so you would have to have a field for that, etc)
– hkotsubo
I just want to change the name and age that already exists
– Julio S
@Julios But if you have already added several, how will you know which one to change? The user has to choose the number, and only then change the data from that specific record
– hkotsubo
@Julios Just to give you an idea of what it would look like: https://jsfiddle.net/7pg5ks9t/
– hkotsubo
It’s more and less like that, I’ll do it in the input but thank you very much give me an idea! , I’m beginner and I’m practicing js
– Julio S