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

221
Visualizações
Manual onChange event collides with JQuery

I have a huge form which dynamically hides or shows elements, depending on the input. For this, I'm using a lot of onChange events. The form can be submitted to, and is loaded using a PHP server. The form itself uses Javascript and JQuery. When the form is loaded, the onChange events are not automatically triggered, so I tried doing it manually with the following code:

//Fires the event manually        
echo 'var element = document.getElementsByName("'.$FieldName.'");';
echo 'event = new Event("change");';
echo 'element.dispatchEvent(event);';

The console tells me that "element.dispatchEvent is not a function". I did some research and tried to put

<script src="DynamicForm.js" type="text/javascript"> <!--Contains the JQuery code--> </script>
<script>$.noConflict(true);</script>
<script> <!--Code that fires the event manually here--> </script>

That still gave me the "element.dispatchEvent is not a function" error. Despite that, I'd have to change the whole JQuery code, which will probably give me some new errors, so I do not necessarily want to do that. Is there a (more efficient) way to solve my issue? I appreciate any assistance.

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

0

The error you are getting is because "element" in your code hasn't been found by your document.getElementsByName code. The use of .getElementsByName is depreciated many years ago. It will also return an array of objects if it matches multiple names.

Instead you should use getElementById() if you want to use pure javascript, or you can achieve what your wanting to do in a single line with jQuery;

$('#formFieldID').trigger("change");

Where formFieldID is the ID of the form field as declared in your html.

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you are trying to manipulate DOM element before their creation.

You can ask jQuery to wait until the DOM is complete with $(function() { /* code */ }) or, a quicker and newer version, $(() => /* code */).

Plus, you can directly trigger an event with jQuery, it will be easier if you use custom event or namespaced event.

echo '$(() => $("[name=\"'.$FieldName.'\""]).trigger("change"));';

To simplify your code, I suggest to use a dedicated class to trigger the event. You can place this directly in the JS code and not in the PHP part.

$(() => $(".js-trigger-change").trigger("change"));
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