简体   繁体   中英

using respond_to format.js to replace the content of a textarea on rails

I have some saved text in my create controller. If it's not stressful, I'd like it to populate a textarea on the page with the saved text along with displaying the error message fields (which is what's already happening). I've used things like replace_html before, but I don't know if there's an easy way to get to textarea or text field IDs and just replace the value of the text. For now I'm just going to do some javascript but it would be nice to know the rails shortcut.

edit: I wasn't in the right mindset because this app uses extjs and was trying to figure out how to do it this way. The text box is actually an ext status bar (same as here : http://www.extjs.com/deploy/dev/examples/statusbar/statusbar-demo.html ) which doesn't get created until the page is finished loading. When I try to enter commnands like the first poster suggested, I get "setvalue is not a function". Playing with firebug on the page I can get it to set the value after loading by using (whatever method to get it through ext or scriptaculous).value = "lol" but none of the page update things work with this.

In your render do |page| you can use the "page <<" to pass inline js so that you can invoke setValue on your texfield element

prototype:

page << "$('texfield_id').setValue('lol');"

page << "$('texfield_id').setValue('#{your_value}');"

# if you like to use rails-like helpers (wich I usually do not) this would also work
page[:textfield_id].setValue your_value 

jquery:

page << "$('#tf_id').val('lol');"

enjoy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM