Skip to main content

Just want to change button text on-click. Coffeescript not making any sense to me.

Javascript • Asked by NicWickman
Hey guys, trying to do a pretty simple thing but all the information I can find online (for "beginners") seems to omit some core things I'm expected to know, because it doesn't even do so much as throw an error. Rails 5.2

I just want to change the text of my button on click.

I have a standard button_to
    <%= button_to "Unsave", resource_path(id: resource.id), remote: true, method: :delete, class: "save_btn", id: "save_btn"%>

I have saved the following to "resources.coffee" in my javascripts folder
$ ->
  $(".save_btn").on "ajax:success", (e, data, status, xhr) ->
   $('.save_btn').text('Save')

In my application.html.erb layout I am loading my JS
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <%= stylesheet_link_tag    'application', media: 'all',
                                              'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'resources', 'data-turbolinks-track': 'reload' %>

Rails threw me an error and instructed me to insert this into assets.rb, which I have done
Rails.application.config.assets.precompile += %w( resources.js )

Nothing happens to the button when I click it. Can somebody please fill in the blanks for me? I have no javascript experience and I am pretty sure I am missing something to the equivalent of "Is it plugged in?".

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.