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
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...
Answer
#3: Post edited
- 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
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
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).