# Rails and prototype: adding a child to an element

**URL:** <https://www.ruby-forum.com/t/rails-and-prototype-adding-a-child-to-an-element/146738>\
**Category:** Rails\
**Created:** [August 24, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/rails-and-prototype-adding-a-child-to-an-element/146738 "2008-08-24T22:15:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [August 24, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/rails-and-prototype-adding-a-child-to-an-element/146738/1 "2008-08-24T22:15:48Z")

</div>

Hello, everybody

A simple question…

I want to add a child to an HTML element. In this case, the element is  
“tbody” and the HTML is a row.

The insert\_html method requires me to specify :after, :before, etc,  
which in this case is not appropriate. The insert method in the new  
version of prototype (1.6) allows you to specify just 2 parameters:  
the id of the element and the content to add to it as a child.  
Optionally, one can specify the position. The Insert.After,  
Insert.Before, etc., are now deprecated (and this is what Rails is  
using internally).

What I ended up doing was something like:

page.call(“myAppendChild”, id, html\_content), which is calling my own  
js function and passing it a template rendered into a string – not  
very clean I think.

Is there a way to append a child to an element from within Rails  
without resorting to my js functions?

Thanks!

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [August 24, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/rails-and-prototype-adding-a-child-to-an-element/146738/2 "2008-08-24T22:50:57Z")

</div>

On 24 Aug 2008, at 21:14, surge [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello, everybody
> 
> A simple question…
> 
> I want to add a child to an HTML element. In this case, the element is  
> “tbody” and the HTML is a row.

replace or replace\_html maps to Element.update which sounds like what  
you want.

Fred

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [August 25, 2008, 2:19am UTC](https://www.ruby-forum.com/t/rails-and-prototype-adding-a-child-to-an-element/146738/3 "2008-08-25T02:19:12Z")

</div>

Fred, thanks for the reply. See in my case, Element.update will not  
work because if I do a replace\_html on the tbody, all the other rows  
will disappear and only the new one will remain. I need to add one row  
in addition to all other rows, whose parent is a tbody. Does it make  
sense?

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [August 25, 2008, 1:22pm UTC](https://www.ruby-forum.com/t/rails-and-prototype-adding-a-child-to-an-element/146738/4 "2008-08-25T13:22:18Z")

</div>

Fred, thanks a bunch. I don’t know how I missed this in the  
description of the four positions. Bottom is exactly what I want.  
Thanks a lot!

On Aug 25, 4:55 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![surge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@surge](https://www.ruby-forum.com/u/surge)\
**Post date:** [August 25, 2008, 10:56am UTC](https://www.ruby-forum.com/t/rails-and-prototype-adding-a-child-to-an-element/146738/5 "2008-08-25T10:56:20Z")

</div>

On Aug 25, 1:18 am, surge [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Fred, thanks for the reply. See in my case, Element.update will not  
> work because if I do a replace\_html on the tbody, all the other rows  
> will disappear and only the new one will remain. I need to add one row  
> in addition to all other rows, whose parent is a tbody. Does it make  
> sense?

then surely insert\_html :bottom is what you want ? Rails has/will be  
updated on edge to use the new Element.insert($('foo), {bottom:  
stuff}) rather than Insertion.xxx

Fred
