# Where to put Javascript?

**URL:** <https://www.ruby-forum.com/t/where-to-put-javascript/82221>\
**Category:** Rails\
**Created:** [January 18, 2007, 10:20am UTC](https://www.ruby-forum.com/t/where-to-put-javascript/82221 "2007-01-18T10:20:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [January 18, 2007, 10:20am UTC](https://www.ruby-forum.com/t/where-to-put-javascript/82221/1 "2007-01-18T10:20:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [January 18, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/where-to-put-javascript/82221/2 "2007-01-18T14:55:37Z")

</div>

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
