2

I have a rails app. It has this button:

<button class="btn btn-primary" onClick="commentSlide()">Comments</button>

I want the button to change color on the click event, but Rails seems to be having trouble reading my JQuery. The first of the two functions below works. When I remove it, I get this error:

 Uncaught ReferenceError: commentSlide is not defined
at HTMLButtonElement.onclick ((index):81)

Here are my two functions. JavaScript works, JQuery does not. Help?

 function commentSlide(){
   alert("hello"); 
  }

 $(document).on('turbolinks:load', function(){

   function commentSlide(){
    $(this).css('backgroundColor', 'red'); 
   }
})

Here is the top section of my javascript file:

//= require jquery
//= require bootstrap-sprockets
//= require jquery_ujs
//= require turbolinks
//= require react
//= require react_ujs
//= require components
//= require_tree .
3

0

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.