# RJS - replacing html for multiple entries

**URL:** <https://www.ruby-forum.com/t/rjs-replacing-html-for-multiple-entries/91889>\
**Category:** Rails\
**Created:** [April 13, 2007, 3:09am UTC](https://www.ruby-forum.com/t/rjs-replacing-html-for-multiple-entries/91889 "2007-04-13T03:09:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [April 13, 2007, 3:09am UTC](https://www.ruby-forum.com/t/rjs-replacing-html-for-multiple-entries/91889/1 "2007-04-13T03:09:39Z")

</div>

I have some elements that could show up on the same page multiple times.  
Can anyone recommend a good way to do a replace\_html in this case? I  
may have, for example, 3 posts that have been repeated on a page, all  
with the same id (e.g,

) and I want to perform a  
replace\_html against all of them. Doing it the normal way just replaces  
the first. Can someone help? Thanks!

---

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [April 13, 2007, 4:58am UTC](https://www.ruby-forum.com/t/rjs-replacing-html-for-multiple-entries/91889/2 "2007-04-13T04:58:41Z")

</div>

Hi,

El Gato wrote:

> I may have, for example, 3 posts that have been repeated on  
> a page, all with the same id (e.g,
> 
> ) and I want  
> to perform a replace\_html against all of them. Doing it the normal  
> way just replaces the first. Can someone help? Thanks!

What you have is invalid HTML / XMTML. DOM ID’s must be unique within a  
page for either Ajax or CSS to work properly (and don’t even get me  
started  
on browser behavior in quirks mode 😉 ). Run your page source against  
[http://validator.w3.org/](http://validator.w3.org/). Assuming you really need to put the same post  
on  
the page multiple times, you’ll need to come up with a new naming  
approach.  
Something like sec1-post-11, sec2-post-11, etc. isn’t tough to either  
render  
or process on return.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [April 13, 2007, 5:35am UTC](https://www.ruby-forum.com/t/rjs-replacing-html-for-multiple-entries/91889/3 "2007-04-13T05:35:38Z")

</div>

Bill W. wrote:

> What you have is invalid HTML / XMTML. DOM ID’s must be unique within a  
> page for either Ajax or CSS to work properly (and don’t even get me  
> started  
> on browser behavior in quirks mode 😉 ). Run your page source against  
> [http://validator.w3.org/](http://validator.w3.org/). Assuming you really need to put the same post  
> on  
> the page multiple times, you’ll need to come up with a new naming  
> approach.  
> Something like sec1-post-11, sec2-post-11, etc. isn’t tough to either  
> render  
> or process on return.
> 
> hth,  
> Bill

Actually, I knew that, and was originally using classes because of it,  
but due to the documentation, thought I’d test id with ids to see if it  
would work. So, is there no way to replace\_html against all elements of  
a class? The problem with just using a unique naming scheme is that I  
want _all_ of the elements of that class affected by the replace… I  
can’t think of a clean way to interrogate the page about all of these  
“unique” ids and change each one then.

---

<div class="post-metadata">

**Author:** ![el-gato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@el-gato](https://www.ruby-forum.com/u/el-gato)\
**Post date:** [April 13, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/rjs-replacing-html-for-multiple-entries/91889/4 "2007-04-13T15:26:34Z")

</div>

Hi,

El Gato wrote:

> Actually, I knew that, and was originally using classes because of it,  
> but due to the documentation, thought I’d test id with ids to see if it  
> would work. So, is there no way to replace\_html against all elements of  
> a class?

replace\_html replaces the content of a single DOM element, specified by  
id.  
To replace the contents of multiple DOM elements, you’d just iterate.  
That’s very easy to do in your rjs template since it allows a mix of  
straight ruby and rjs.

> The problem with just using a unique naming scheme is that I  
> want _all_ of the elements of that class affected by the replace… I  
> can’t think of a clean way to interrogate the page about all of these  
> “unique” ids and change each one then.

Not sure what you mean by ‘interrogate the page’. It sounds like maybe  
you’re coming from a background that’s mostly client-side js. In RoR,  
every  
thing that’s on the page, you put there from the server side where it’s  
very  
easy to keep track of. So I typically don’t think about interrogating  
the  
page, I think about interrogating the database or the session store to  
find  
out what’s on the page. In the example you’ve given so far, my  
impression  
is you’ve got one ‘thing’ that appears in different sections of the  
page.  
One (not tested, and it’s early here 😉 ) approach to replacing all  
those  
elements could look something like…

sections = [‘sec1-’, ‘sec2-’, 'sec3-]  
sections.each do {|this\_section|  
element\_to\_replace = this\_section+@post\_to\_replace  
page.replace\_html(element\_to\_replace, …  
}

I’m assuming the @post\_to\_replace was identified in the controller  
method.  
The sections could, of course, be identified there too.

hth,  
Bill
