# Javascript code blocks in RJS

**URL:** <https://www.ruby-forum.com/t/javascript-code-blocks-in-rjs/121464>\
**Category:** Rails\
**Created:** [November 28, 2007, 6:12am UTC](https://www.ruby-forum.com/t/javascript-code-blocks-in-rjs/121464 "2007-11-28T06:12:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [November 28, 2007, 6:12am UTC](https://www.ruby-forum.com/t/javascript-code-blocks-in-rjs/121464/1 "2007-11-28T06:12:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [November 28, 2007, 7:12am UTC](https://www.ruby-forum.com/t/javascript-code-blocks-in-rjs/121464/2 "2007-11-28T07:12:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [November 28, 2007, 7:50am UTC](https://www.ruby-forum.com/t/javascript-code-blocks-in-rjs/121464/3 "2007-11-28T07:50:46Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [November 28, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/javascript-code-blocks-in-rjs/121464/4 "2007-11-28T16:44:01Z")

</div>

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:

> 

–  
Sincerely,

William P.
