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

209
Visualizações
How to separate the "press-key" from the "click-mouse" event to prevent submission when press-key?

I have the "chips" form link you can see in the attached image here and I want to create a condition such as if someone press "Enter" he can note the item added but what happens here is that form sends the data to backend.

image description here

I tried many times to modify the behavior using the "event" but it didn't work you can see the approaches I sticked with:

Approach1:

// input to submit job
const submit_job = document.getElementById("submit-job");
submit_job.onclick = (e) => {
    e.preventDefault();
    if (e.key !== "") {
        console.log(e.currentTarget)
    } else if (e.pointerType === "mouse") {
        submit_job.onsubmit = () => {
            return true
        }
    }
}

// This approach won't work because I found that "e.key" worked only when typing by Keyboard

Approach 2:

function getEventType(event) {
    if (event.type === "keydown") {
        event.preventDefault();
    }
}

submit_job.addEventListener('keydown', getEventType, true);

submit_job.addEventListener('click', getEventType);

so, any help in that issue, please?

Note:

No need to see the full code because the code is working fine but when I place it into the form this problem occurs so, you can spire me using that variable only "submit_job" in your answer.

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

0

Your implementation of the form submission does not seem like a good idea. You should not prevent users from submitting the from when pressing "Enter". Clicking "Enter" should trigger a form submission which is common for most forms. Why not add a unique button beside the input field that when clicked allows the user to enter a note or text? If you take your current approach users will only be able to submit by clicking the submit button which is bad practice. I suggest creating an additional button and adding an event handler to that button so you have a button that adds notes when clicked, so pressing "Enter" will not clash with what you are trying to accomplish.

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