0

I have the following code in my view.

<script>
  function show_alert() {
    alert()
  }
</script>
<a href="#" data-turbo="false" onclick="show_alert()">Alert a</a>
<p onclick="show_alert()">Alert p</p>

My p tag works, but not the link which errors in the console with "show_alert is not a function". I know that Turbo intercepts links and forms, but I was expecting data-turbo="false" to disable it.

I'm gradually learning Turbo, so I do not want to completely disable it.

Vantalk
  • 367
  • 2
  • 5
  • 21
  • 1
    Why don't use stimulus instead of inline JS-attributes? – mechnicov Apr 15 '23 at 09:54
  • This code doesn't reproduce whatever your issue is. Both clicking the link and the

    tag will create a alert box. The whole approach with inline scripts and attributes is a dead end though.

    – max Apr 15 '23 at 11:08
  • 1
    If I had to guess the issue is that you're not actually using the code above and you have a scoping issue since `show_alert` is not in the global scope. You can either waste time figuring out how to make it global or spend that time learning how to actually write JS in 2023. – max Apr 15 '23 at 12:06
  • @vantalk I tried your code in my Ruby on Rails application, and it is working properly. It is showing an alert box in both the link and p tags. Hence, try to provide some more details so we can help you. – Rutik Patel Apr 15 '23 at 13:14
  • The code I've posted is obviously a oversimplification highlighting inaccessibility/scope, yet still a valid test. Front-end frameworks are overkill for sites with just two dynamic functions, also a mess with DOM duplication, managing two states, etc. I'd please ask for answers to the subject instead of indications on how to write "modern" JS. @RutikPatel What rails and ruby versions are you using? I used Rails 7.0.3 and Ruby 3.0.2. Thank you! – Vantalk Apr 16 '23 at 19:28
  • 1
    @Vantalk , I am Using Rails 7.0.4 and Ruby 3.1.2. – Rutik Patel Apr 17 '23 at 09:48

0 Answers0