Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
How to prevent entering more value when I press enter using keydown event

Sorry in advance if I'm not making any sense. I'm a complete beginner to coding. I'm creating a shopping list and a user will enter the items through placeholder and will click or press enter to add the items in the list.

The click event is working properly.

Edit

enter image description here

var button = document.getElementById("enter");
var input = document.getElementById("userinput");
var ul = document.querySelector("ul");

button.addEventListener("click", function() {
    if (input.value.length > 0) {
        var li = document.createElement("li");
        li.appendChild(document.createTextNode(input.value));
        ul.appendChild(li);
        input.value = "";
    }
})

document.addEventListener('keydown', function (event) {
    if (input.value.length > 0, event.key === 'Enter') {
        var li = document.createElement("li");
        li.appendChild(document.createTextNode(input.value));
        ul.appendChild(li);
        input.value = "";
    }
  }); 
<input type='text' id='userinput' />
<button type='button' id='enter'>Enter</button>

<ul></ul>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Can you elaborate more on your question? Also, stick to the "DRY" principle. You have both the button and the input adding a item then adding it to the list: Make a function that takes the input and call it on both the button and the input event:

function addListItem(input){
    var li = document.createElement("li");
    li.appendChild(document.createTextNode(input.value));
    ul.appendChild(li);
    input.value = "";
};

It'll look like this:

        button.addEventListener("click", function() {
        if (input.value.length > 0) {
            AddListItem(input);
        }
    });
    input.addEventListener('keyup', function (event) {
        if (input.value.length > 0 &&  event.key === 'Enter') {
            AddListItem(input);
        }
      });
about 4 years ago · Juan Pablo Isaza Relatório

0

// So, If I'm getting it correct. You have an input field and you're //reading values from that field // So add 'keyup' to input element. // Fix the IF // control the input length in order to control values?

I need more info on this: How to prevent entering more value when I press enter using keydown event

        var input = document.getElementById("userinput");
        var ul = document.querySelector("ul");
        var btn = document.querySelector('#enter')
        
        btn.addEventListener("click", function() {
            if (input.value.length > 0) {
                var li = document.createElement("li");
                li.appendChild(document.createTextNode(input.value));
                ul.appendChild(li);
                input.value = "";
            }
        })
        
        input.addEventListener('keyup', function (event) {
            //basically, if the input value is falsy('') you return nothing or false (abort)
            if(!input.value) {
                return;
                //return false;
                //alert('cannot add empty list item')
              }

            if (input.value.length > 0 &&  event.key === 'Enter') {
                var li = document.createElement("li");
                li.appendChild(document.createTextNode(input.value));
                ul.appendChild(li);
                input.value = "";
            }
          }); 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda