0

I have some questions on jquery in rails3. If I put a function like this in my application.js. Is it appropriate to put my JS in there rather than in my views?

$(function())
{
$("alert").onclick(function());
....
});

How do I call it within a view? Doesnt the function need an identifier or name? Is link_to_function a good way?

1 Answer 1

1

Proper way of doing it:

$(function()
{
  $(".alert").click(function( .... ));
  ....
});

Don't put this in your views. Just add to to application.js

This attaches a click handler to your links with the class alert. Function will be called automatically when the click event fires (when you click the link).

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

1 Comment

Yes, that's correct. Add a CSS class with the same name to the link that you want jQuery to attach your event handler to. For single items you could also us and ID.

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.