I'm a newbie ruby on rails developer. I've just created my first application and made use of the the bootstrap
carousel. Now, I want to add some javascript
. My application does not have any custom javascript
at this point. What I want to do is when the left
or right
button on the carousel is pressed, I want an alert
to pop up. Preferably, I want to include my javascript
file in the /assets/javascript
directory. The purpose of this is to just learn how to add new javascript
files in my application.
- 1Check this out: railsapps.github.io/rails-javascript-include-external.html– SamCommentedDec 6, 2015 at 5:51
Add a comment |
3 Answers
Adding your custom javascript to your rails app is real easy, just follow the steps below :-
- create a file (say custom.js) under app/assets/javascripts/
- Open up your manifest file i.e application.js (this is the file where you need to include every javascript file you want to load into your rails app, add the script name to it //=require custom.
- Visit your rails app from browser and check the page source and search for custom.js being loaded or not thats all.
For Rails 6
Add your javascript code under /app/javascript//file.js
and then in app/packs/application.js
require("<folder name>/file")
Or Just Do This
<title>box vol</title> </head> <body> <form id="frm1" action="Calculate.html"> <table width="460px" border="1px"> <tr> <th colspan="2">Box Volume Calculation</th> </tr> <tr> <td>Width</td> <td> <input type="number" id="width" placeholder=""> Millimeters</td> </tr> <tr> <td>Depth</td> <td> <input type="number" id="depth" placeholder=""> Millimeters</td> </tr> <tr> <td>Height</td> <td> <input type="number" id="height" placeholder=""> Millimeters</td> </tr> <tr> <td>Box Volume : Litres<p id="test"></p></td> <td> </td> </tr> </table> <br> <input type="button" onclick="Calculate()" value="calculate"> </form> </body> </html> <script type="text/javascript"> function Calculate() { var wide = document.getElementById("width").value; var deep = document.getElementById("depth").value; var high = document.getElementById("height").value; var total = wide * high * deep/1000000; document.getElementById("test").innerHTML = total; } </script>
- Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.– AdriaanCommentedApr 20, 2023 at 12:05