html - Changing InnerHTML with a helper method in rails -


i'm pretty new rails, , have website has js function uses onchange method <select>. code kind of this:

var text = document.getelementbyid("dude");

text.innerhtml = "bro";

the thing is, want rewrite in ruby. there way use helper method pass argument , change value of html?

<p><%= text = "hey" %></p>

<% helper_method(text) %>

i've been looking around answer , saw rjs? not sure if related, don't know start.

thanks

javascript

the big problem have you're confusing roles of ruby & js.

ruby server-side language; runs on server compile pure html browser. html passed front-end client (browser), rendered.

--

ruby == php -- cannot interactively change elements after "dom" has loaded. javascript can, , why you'll not able translate code js.

the only way capture onchange (events) use js / jquery handle them:

onchange="return js_function();" 

(java)scripts embedded in or included html pages , interact document object model (dom) of page

ruby can when html rendered on server. js way bind events elements.... however way interpret events can harness ruby.


ajax

using ajax (asynchronous javascript , xml) allows send requests server, appending result dom. server-side processing would induce ruby, allowing use provide appropriate response:

#html <%= link_to "x", "path", onchange: "return js_function();" %>  #app/assets/javascripts/application.js function js_function() {    var value = // ajax //    var text = document.getelementbyid("dude");    text.innerhtml = value; } 

i don't know you're trying do; using ajax pure js pretty difficult; jquery has own $.ajax function handle if wish (to drink devil's cup):

#app/assets/javascripts/application.js function js_function() {    var value = $.get("your/path");    var text = document.getelementbyid("dude");    text.innerhtml = value; }  #config/routes.rb resources :your   :path, on: :collection end  #app/controllers/your_controller.rb class yourcontroller < applicationcontroller    def path      respond_to |format|        format.js { render text: "tester" }      end    end end 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -