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

295
Visualizações
Jquery .append() appends on every event

I have very minimal javascript and jquery knowledge, so the problem is, when click with mouse "li" tab and press ENTER jquery attach .append() on every mouse click event.

$(document).on("click", function(event){
        let $ul_elem = $(event.target).parent().parent();
        const $li = '<li><input type="text" name="geras"></li>'
        
        if($(event.target).parent().prop("localName")=="li"){ 
            console.log($(this.event))
            $(window).on("keydown", function(event){
                if(event.keyCode == 13) {
                    event.preventDefault();
                    $($ul_elem).append($li);
                    $($ul_elem).children().last().children().focus();   
                    return false
                }
            })
        }
        else{
            $(window).unbind("keydown");
        }      
    })

in result if i click like 5 times with my mouse on the same li tab, it will create 5 new const $li elements and i dont want that.

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

0

You should avoid binding event handlers in context of another event handler. Your code currently rebinds the keydown handler on each click (when the if statements succeeds.) This is why the code appends several elements on each Enter. I guess you want to append li elements to ul elements conditionally. If yes this is one way of doing it:

$(document).on("click", "ul", function() {
     $(this).toggleClass('active');
});

$(window).on("keydown", 'ul.active', function(event) {
     if (event.keyCode == 13) {
         const $ul_elem = $(this);
         const li = '<li><input type="text" name="geras"></li>'
         event.preventDefault();
         $ul_elem.append(li);
         $ul_elem.children().last().children().focus();   
         return false
     }
})

This makes the keydown handler work conditionally by using event delegation. In case you want to listen to the keydown event of input elements you can just code $(document).on("keydown", 'input[type=text]', function(event) { ... and there is no need to use click handler. I'm still not sure what you are trying to achieve though.

what I want to do is after i put text in my default li > input field and press ENTER i want to create new li in ul

This should do it:

$(document).on("keyup", 'li input[type=text]', function(event) {
    if (event.keyCode == 13) {
        const $ul_elem = $(this).closest('ul');
        const li = '<li><input type="text" name="geras"></li>'
        event.preventDefault();
        $ul_elem.append(li);
        $ul_elem.children().last().children().focus();   
    }
})
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