question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Turbo visits don't set `:target` to the targeted element

See original GitHub issue

When doing a Turbo visit to a link that has an anchor #, the targeted element does not have the :target pseudo-class.

Steps to reproduce

  1. In the Turbo project, visit http://localhost:9000/src/tests/fixtures/navigation.html
  2. Search for elements with :target pseudo-class: $(':target') returns null
  3. Reload page
  4. Search for elements with :target pseudo-class: $(':target') returns element with id element-id

Expected behavior

After clicking a link with an anchor, it’s expected that the element which matches the URL hash has the :target pseudo-class.

This is important for apps that use that selector to add an indicator to the element - like navigating to a specific comment in a GitHub Issue:

image

We are happy to look into this if we can get some pointers on where hashes may be getting interfered with 😃

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
domchristiecommented, May 25, 2022

Another option might be to create a link element behind-the-scenes

This seems to work:

addEventListener('turbo:load', target)
addEventListener('hashchange', target) // for same-page navigations

function target () {
  if (location.hash) {
    const a = document.createElement('a')
    a.href = `#${location.hash.slice(1)}`
    a.click()
  }
}
0reactions
straight-shootacommented, Oct 11, 2022

I’ve encountered this problem as well when :target CSS selectors stopped working with turbo navigation. The workaround triggering an on-page navigation in the browser seems to work well (https://github.com/hotwired/turbo/issues/592#issuecomment-1137827028).

I think it would be great if turbo provided this functionality directly. It’s supposed to replace native browser navigation, so it should also cover all facets of that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Turbo.visit() and target a specific frame?
I've verified through console output that the src is set correctly. Any suggestions? Make sure to call reload after setting the source.
Read more >
Dynamic forms with Turbo - Thoughtbot
The responsibilities of the element controller's click action are extremely limited: click any elements marked as a “click” target. // app/ ...
Read more >
Digging into Turbo with Ruby on Rails 7 - Web-Crunch
We'll be using this to target Turbo Stream updates. This lets the app know where to update DOM elements on a given page....
Read more >
Using Hotwire Turbo in Rails with legacy JavaScript
The turbo-visit-control meta tag forces Turbo to make a full page reload if it encounters an AJAX response (initiated outside of a Turbo...
Read more >
Using Rails Turbo Frames - The Airship Blog
Turbo Frames is a set of tools that can replace complex, ... control when it came to targeting when and how specific elements...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found