Hi all: I have a block of javascript that I want to use to clear out field notices in multiple forms on my site. The way I''m doing it now is to write directly to RJS like render :update do |page| page << JS_BLOCK end with the code block assigned as a string to a variable at the ApplicationController level like JS_BLOCK = "var notice_fields $(''reg_form'').getElementsByClassName(''field_notice''); for(var i=0;i<notice_fields.length;i++) notice_fields[i].innerHTML= ''''" Is there a better way of handling reused js code within RJS? Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You could store the commands in an rjs template and keep it in a shared directory. Another idea is to give all of these fields a css class name that is the same the use the select prototype call to do it in one fell swoop using a collection proxy: render :update do |page| page.select(''.notice_fields'').each do {|f| f.replace_html ''''} end As long as all of the elements have class="notice_fields" you are good to go. Note that it can be any one of a number of classes for those elements or any other valid css selector. -Bill mofo mofo wrote:> Hi all: > > I have a block of javascript that I want to use to clear out field > notices in multiple forms on my site. > > The way I''m doing it now is to write directly to RJS like > > render :update do |page| > page << JS_BLOCK > end > > with the code block assigned as a string to a variable at the > ApplicationController level like > > > JS_BLOCK = "var notice_fields > $(''reg_form'').getElementsByClassName(''field_notice''); > for(var i=0;i<notice_fields.length;i++) notice_fields[i].innerHTML= ''''" > > Is there a better way of handling reused js code within RJS? > > Thanks. >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the quick response Bill. I really appreciate it. f.replace_html didn''t work since replace_html is not a Prototype Element method, but I used Element.update instead. So the working syntax is: render :update do |page| page.select(''.field_notice'').each { |f| f.update '''' } end Thanks again! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
No problem. Sorry about the replace_html bit as I should have caught that, but I was tired and headed to bed :) Glad you got it to work. -Bill mofo mofo wrote:> Thanks for the quick response Bill. I really appreciate it. > > f.replace_html didn''t work since replace_html is not a Prototype Element > method, but I used Element.update instead. So the working syntax is: > > render :update do |page| > page.select(''.field_notice'').each { |f| f.update '''' } > end > > Thanks again! >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---