9

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.

1

3 Answers 3

15

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.
0
    5

    For Rails 6

    Add your javascript code under /app/javascript//file.js

    and then in app/packs/application.js

    require("<folder name>/file")

      0

      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> 
      1
      • 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.
        – Adriaan
        CommentedApr 20, 2023 at 12:05

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.