Where to put Javascript?

I want to add some simple JS code to my application that registers an
‘onclick’ event for a form field and calls a function to change various
DOM elements.

Where is the best place to put this?

In the layout for the view? In application.js?

Thanks.

As I’m sure you’re aware, Javascript can go basically anywhere, and
there is
no set place to put said JS. If the script is to work and be available
for
every page, either put it in your layout, or if it’s complicated (or
there
are multiple layouts), then yeah, put it in the public/javascripts
folder.
If it’s simple, small functions and you want to keep the source clean,
then
you can do something like this:

in layout:

... ...

and in your rhtml:

<% content_for(:javascript) do %>
put javascript stuff here
<% end %>

Hope that helps.

Jason

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs