Turbo visits don't set `:target` to the targeted element
See original GitHub issueWhen doing a Turbo visit to a link that has an anchor #
, the targeted element does not have the :target
pseudo-class.
Steps to reproduce
- In the Turbo project, visit http://localhost:9000/src/tests/fixtures/navigation.html
- Search for elements with
:target
pseudo-class:$(':target')
returnsnull
- Reload page
- Search for elements with
:target
pseudo-class:$(':target')
returns element with idelement-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:
We are happy to look into this if we can get some pointers on where hashes may be getting interfered with 😃
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This seems to work:
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.