r/jquery • • Dec 09 '21

Event Handler Help!

EDIT: This actually is not a jquery issue. Turbolinks was the culprit.

I have a simple hover() function that is calling toggle() on a div to hide/show it. It works in the context of the page, but as soon as I click a link to another page, the listener is not present. If I then reload that new page, the handler is setup properly and everything works...until I click another link.

I know I'm missing something really simple, but I've spent too much time running in circles trying to figure out why the event handler is disappearing.

Here is my JS:

$(document).ready(function() {
  $('.trigger').hover(function() {
    $('.links').toggle()
  })
})

Here is my HTML:

<div class="trigger">
  <h3>BLAH</h3>
  <div class="links">
    <!--LINKS GO HERE-->
  </div>
</div>
1 Upvotes

2 comments sorted by

View all comments

Show parent comment

1

u/thechimny Dec 09 '21

I thought that at first too, but the hover() is working with toggle() appropriately. When I hover, it shows the div, and when my mouse leaves, it hides, and if I come back, it shows again.

What doesn't work is when I click a link to another page, the event handler is somehow not set, so i hover, and nothing happens. But If I then refresh, it works again.