How I make a name that were created count 1

Asked

Viewed 35 times

0

I am trying to add the names to count a number which have been created, under the title name this number 3 right ? I know number three is because I got the letter number. But how do I do it ?


  • Example 1 - yes 2 - yes

let a = []

function botao() {
  var nome = document.getElementById("nome")
  var r = document.getElementById("res")
  if (nome.value == "") {
    alert("Por favor digite um nome!")
  }
  if (nome.value.length) {
    a.push(nome)
    r.innerHTML += ` <tr><th scope="row">${nome.value.length}</th> <td>${nome.value}</td>`
  }
}
<div class="container">
  <h1 class="text-center mt-3">
    Agenda
  </h1>
</div>
<div class="container">
  <table class="table">
    <thead>
      <tr>
        <th scope="col">Nome</th>
      </tr>
    </thead>
    <tbody id="res">
    </tbody>
  </table>
</div>
<div class="text-center">
  <h1 class="text-center mt-2 mb-4">Registe por aqui</h1>
  <input type="text" id="nome" name="nome" placeholder="NOME">
  <input type="submit" id="btn" onclick="botao()">
</div>

  • 1

    Look I read 2x and I can’t understand what you want to do

  • Calm down, you saw in the image below the name has a number (3) right? I want to do type first that was created called (1-Ricardo) second time (2 - Julio) understood? but in the image that this (3 - yes)

1 answer

2


As you are changing the size of the array, each time you place an item, you can change the word size

r.innerHTML += ` <tr><th scope="row">${nome.value.length}</th> <td>${nome.value}</td>`

by the size of the array:

r.innerHTML += ` <tr><th scope="row">${a.length}</th> <td>${nome.value}</td>`
  • That’s right, thank you beast!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.