simple JavaScript in rails -


i wanted trigger alert function when click html tags.

# app/assets/javascripts/home.js  function alertbox() {   window.alert("gogo"); } document.getelementbyid("java-test").onclick = function() { alertbox() };   # app/views/home/index.html.erb  <h1>please <span id="java-test">pop</span> up~</h1> 

it's working when use <span onclick="alertbox()"> wanted make html files little bit clearer. missing?

i think i'm totally wrong on understanding how works on ruby on rails. of comments huge me. thanks!

it's because dom isn't ready, span isn't there script find when getelementbyid. try this, instead:

# app/assets/javascripts/home.js  function alertbox() {   window.alert("gogo"); } window.onload = function () {     document.getelementbyid("java-test").onclick = function() {         alertbox();     }; };   # app/views/home/index.html.erb  <h1>please <span id="java-test">pop</span> up~</h1> 

Comments

Popular posts from this blog

javascript - Add class to another page attribute using URL id - Jquery -

android - MPAndroidChart - How to add Annotations or images to the chart -

IF statement in MySQL trigger -