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

195
Visualizações
Rails 6.1 - JS stops working after clicking on any button

I have a few JS functions linked to application.html.erb through channels.

They all work fine when the page is loaded. However, if I click any button on my page (href=""), JS seems to stop working and none of my functions apply anymore.

Here's the application.html.erb file:

<!DOCTYPE html> <html>   <head>
    <title></title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload', defer: true %>

    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>   </head>

  <body>
    <%= render 'shared/navbar' %>
    <%= yield %>
    <%= render 'shared/footer' %>
    <%= render 'shared/modal' %>   </body> </html>

One of the functions:

var dropdownMenu = document.querySelector(".dropdown-menu");

function triggerDropdown() {

  dropdownMenu.addEventListener("click", (event) => {

    var dropdownReveal = document.querySelector(".dropdown-container");
    var closingBtn = document.querySelector(".closing-btn");
    var openingBtn = document.querySelector(".dropdown-menu img");

    if (dropdownReveal.style.display == 'block') {
      dropdownReveal.style.display = 'none';
      closingBtn.style.display ='none';
      openingBtn.style.display = 'block';
      
    } else {
      dropdownReveal.style.display = 'block'
      closingBtn.style.display ='block';
      openingBtn.style.display = 'none';
      
    };
  });
}

triggerDropdown();

The application.js file:

 // This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.

import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"

Rails.start()
Turbolinks.start()
ActiveStorage.start()

And the index.js:

// Load all the channels within this directory and all subdirectories.
// Channel files must be named *_channel.js.

const channels = require.context('.', true, /_channel\.js$/)
channels.keys().forEach(channels)

Any help would be highly appreciated :)

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

0

It's hard to tell just like that.

Have you noticed the point where it started to disfunction ? What have you implemented before it broke ? Also the bit inside "One of the functions" does not seem to appear anywhere in your application.js . How do you include this JS to your pages ?

Though I have a guess :

Because of Turbolinks, your JS permanently sits on your page and is not rerun each time new content is replaced in your DOM, then you have to force tell Turbolinks to rerun some bits of JS. You may then have to encapsulate your triggerDropdown() function into

document.addEventListener("turbolinks:load", function() { 
.... your code ....
};

( https://github.com/turbolinks/turbolinks#observing-navigation-events ) so that the navbar which has been replaced on a link visit, is parsed again through JS

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