Bubibob Bubibob - 3 months ago 13
Javascript Question

Basic javascript ; alert on click

Trying to teach myself how to use js in rails and I found a good video on the topic but somehow it does not work for me and I can not figure out why.

app/views/static_pages/help.html.erb:

...
<p><%= link_to "Description", "#", id: "desc-link" %></p>
...


app/javascripts/static_pages.coffee:

$(document).on "page:change", ->
$("#desc-link").click ->
alert "Done!"


If I click on the link, nothing happens (no error either).

Answer

In rails 5.

I restarted server after add require jquery

my working code

$(document).on('turbolinks:load', ->
    $("#desc-link").click ->
        alert("Done!")
      )

or

$(document).on('turbolinks:load', ->
    $("#desc-link").on 'click', ->
        alert("Done!")
      )