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

150
Visualizações
JQuery change event listener working in Chrome, but not in Firefox

I got a web page with a long table to display data. Using JQuery Tablesorter I made the table sortable.

The last column of the page contains checkboxes (with the class chk), to allow the user to select multiple entries. In the table header there is also a checkbox (with the id checkall and no class). By clicking this checkbox, the values of all to other checkboxes should flip. As the page needs some time to load fully, I am using the $(window).load() function instead of $(document).ready().

I got the following JavaScript code:

$(window).load(function() {
    $('#checkall').change(function() {
        if ($('#checkall').is(":checked")) {
            $('.chk').prop('checked', true);
        } else {
            $('.chk').prop('checked', false);
        }
    });
});

This works as expected in Chromium, but not in Firefox. After playing around with the code it seems like the Change-Listener is not even set in Firefox. Nothing that I put into the inner function code is executed on checking the checkbox, while the $(window).load() function is executed as expected. Same behavior happens when I add an click() handler instead or using on("change, function(){...}).

Is this a JQuery bug or am I doing something wrong?

I'm using JQuery 3.6.0 with JQuery Migrate 1.4.1.

Update:

I found out that this seems to be a timing issue / race condition. If I reload the page sometimes, it works in Firefox as well in about 1 of 10 tries. But I have still no idea what the cause is and how to fix it.

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

0

Here is an approach that uses event delegation. You need an element that is present in DOM at the time document.ready is fired. Lets say that element is the table. You would write:

$(function() {
    $("#the-table").on("click", "#checkall", function() {
        // toggle stuff
    });
});

In this case the event handler is bound to the table. It will trap all clicks and, if the originator of the event is the checkbox it will fire the callback.

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