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

152
Visualizações
Update field after changing its value with JavaScript

I would like to simulate user input on a website using JavaScript. To do so, I simply do something like this:

a = document.getElementsByClassName('field-name')[0];
a.value = 'new value';

However, on some websites which require you to click "Send" after the input, the button remains inactive (sorry for not providing the example, the only sites with that feature that I know of require logging in).

I remember that I once fixed that problem by "Updating" the element afterwards, but I don't remember exactly how. I have tried adding this:

const change = new InputEvent('change');
a.value = 'new value';
const isNotCancelled = a.dispatchEvent(change);

But no luck here. Any ideas how to overcome this?

Update: Just as an example: here is a website which does what I described. Just click the chat icon on the bottom right and input random stuff for email and etc., then you will see the input box with the button that I described.

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

0

The InputEvent's type should be either beforeinput or input (input, in your case), not change; you might also consider firing a an Event at it with type set to "change" (there is no specific ChangeEvent constructor). For the input event, be sure you set bubbles: true since the default for the constructor is false but input events bubble (change events don't, although some libraries make them do so). You might also need to blur the field, and you might focus it at the start (so the blur does something).

For instance:

const a = document.getElementsByClassName("field-name")[0];
// Or: const a = document.querySelector(".field-name");
a.focus();
a.value = "new value";
a.dispatchEvent(new InputEvent("input", {bubbles: true, cancelable: true}));
a.dispatchEvent(new Event("change", {bubbles: false, cancelable: true}));
a.blur();

If none of those work, find the button and set its disabled to false.

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