Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+1 −1
Q&A How to call a function from ruby-on-rails (HTML)?

Hope I understood what do you mean. I guess for example you have app.js (which contains your JavaScript function) and app.html.erb (which you want to call JavaScript function from). OK. In your ap...

posted 3y ago by Kevin M. Mansour‭  ·  edited 3y ago by Kevin M. Mansour‭

Answer
#3: Post edited by user avatar Kevin M. Mansour‭ · 2021-07-08T18:11:11Z (over 3 years ago)
Cleaning Up.
  • Hope I understood what do you mean. I guess for example you have `app.js` (which contains your JavaScript function) and `app.html.erb` (which you want to call JavaScript function from).
  • OK. In your `app.html.erb` contain:
  • ```ruby
  • <%= javascript_include_tag('app.js') %>
  • <%= button_tag "Test", id: "TestBTN" %>
  • ```
  • As you seen above, first I linked our Ruby HTML file to our nice JavaScript file then I have made a simple button and I added an ID to it (which is `TestBTN`) to check `onclick` from our JavaScript funtion.
  • Then in your `app.js`:
  • ```javascript
  • document.getElementById("TestBTN").addEventListener("click", function() {
  • console.log("Done!");
  • });
  • ```
  • As seen above, I have made an `EventListener` for our button and `onclick` it will print "Done!" in the console.
  • Hope this helps!
  • More about [`javascript_include_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-javascript_include_tag).
  • More about [`button_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-button_tag).
  • Hope I understood what do you mean. I guess for example you have `app.js` (which contains your JavaScript function) and `app.html.erb` (which you want to call JavaScript function from).
  • OK. In your `app.html.erb`:
  • ```ruby
  • <%= javascript_include_tag('app.js') %>
  • <%= button_tag "Test", id: "TestBTN" %>
  • ```
  • As you seen above, first I linked our Ruby HTML file to our nice JavaScript file then I have made a simple button and I added an ID to it (which is `TestBTN`) to check `onclick` from our JavaScript funtion.
  • Then in your `app.js`:
  • ```javascript
  • document.getElementById("TestBTN").addEventListener("click", function() {
  • console.log("Done!");
  • });
  • ```
  • As seen above, I have made an `EventListener` for our button and `onclick` it will print "Done!" in the console.
  • Hope this helps!
  • More about [`javascript_include_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-javascript_include_tag).
  • More about [`button_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-button_tag).
#2: Post edited by user avatar Kevin M. Mansour‭ · 2021-07-08T18:07:57Z (over 3 years ago)
Cleaning Up.
  • Hope I understood what do you mean. I guess for example you have `app.js` (which contains your JavaScript function) and `app.html.erb` (which you want to call function from).
  • Then your `app.html.erb` contain:
  • ```ruby
  • <%= javascript_include_tag('app.js') %>
  • <%= button_tag "Test", id: "TestBTN" %>
  • ```
  • As you seen above, first I linked our Ruby HTML file to our nice JavaScript file then I have made a simple button and I added an ID (which is `TestBTN`) to check `onclick` it from our JavaScript funtion.
  • Then your `app.js` contain:
  • ```javascript
  • document.getElementById("TestBTN").addEventListener("click", function() {
  • console.log("Done!");
  • });
  • ```
  • As seen above, I have mande an `EventListener` for our button and `onclick` it will print "Done!" in the console.
  • More about [`javascript_include_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-javascript_include_tag).
  • More about [`button_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-button_tag).
  • Hope I understood what do you mean. I guess for example you have `app.js` (which contains your JavaScript function) and `app.html.erb` (which you want to call JavaScript function from).
  • OK. In your `app.html.erb` contain:
  • ```ruby
  • <%= javascript_include_tag('app.js') %>
  • <%= button_tag "Test", id: "TestBTN" %>
  • ```
  • As you seen above, first I linked our Ruby HTML file to our nice JavaScript file then I have made a simple button and I added an ID to it (which is `TestBTN`) to check `onclick` from our JavaScript funtion.
  • Then in your `app.js`:
  • ```javascript
  • document.getElementById("TestBTN").addEventListener("click", function() {
  • console.log("Done!");
  • });
  • ```
  • As seen above, I have made an `EventListener` for our button and `onclick` it will print "Done!" in the console.
  • Hope this helps!
  • More about [`javascript_include_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-javascript_include_tag).
  • More about [`button_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-button_tag).
#1: Initial revision by user avatar Kevin M. Mansour‭ · 2021-07-08T18:05:13Z (over 3 years ago)
Hope I understood what do you mean. I guess for example you have `app.js` (which contains your JavaScript function) and `app.html.erb` (which you want to call function from). 

Then your `app.html.erb` contain:

```ruby
<%= javascript_include_tag('app.js') %>
<%= button_tag "Test", id: "TestBTN" %>
```

As you seen above, first I linked our Ruby HTML file to our nice JavaScript file then I have made a simple button and I added an ID (which is `TestBTN`) to check `onclick` it from our JavaScript funtion.

Then your `app.js` contain:

```javascript
document.getElementById("TestBTN").addEventListener("click", function() {
  console.log("Done!");
}); 
```
As seen above, I have mande an `EventListener` for our button and `onclick` it will print "Done!" in the console.

More about [`javascript_include_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-javascript_include_tag).

More about [`button_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-button_tag).