0

I have couple javascript functions that attaching action to buttons in controller view some/index. I navigate to some/index using link_to method. But when i'm using link_to method, buttons has no attached javascript actions (in some/index view). When i try open some/index directly from my browser, buttons have all attached actions i need and working fine.

some.js.coffee

jQuery ->
    $('.my-btn').on 'click', (event) ->
    ...

I think it can't attach actions to buttons in some/index when i'm using link_to because it already executed all javascript in root/index view and page is not refreshing when i'm using link_to. How can i solve this problem? Still want to use link_to)

1 Answer 1

2

This is a result of Turbolinks. You can get around this with the following:

$(document).on 'click', '.my-btn', (event) ->
  ...

This is happening because Turbolinks is loading the page, and there isn't a document ready event being fired. You want to wait until Turbolinks fires the page:load event.

You do not need the jQuery -> with the above code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.