# Generating new div when link is clicked

**URL:** <https://www.ruby-forum.com/t/generating-new-div-when-link-is-clicked/176489>\
**Category:** Rails\
**Created:** [October 1, 2009, 9:40am UTC](https://www.ruby-forum.com/t/generating-new-div-when-link-is-clicked/176489 "2009-10-01T09:40:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan8720](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@ryan8720](https://www.ruby-forum.com/u/ryan8720)\
**Post date:** [October 1, 2009, 9:40am UTC](https://www.ruby-forum.com/t/generating-new-div-when-link-is-clicked/176489/1 "2009-10-01T09:40:47Z")

</div>

Hi everyone,

I’m new to rails, and I’ve got my application up and running. Now I’d  
like to add a function that I haven’t been able to find information  
on.

Basically, I start with a form that asks the user to input the number  
of widgets of some type they have.

For example (\_\_\_\_ is a textfield):

\_\_\_\_\_ widgets of type \_\_\_\_\_\_.

Then I want to have a link below this that says “Add more widgets.”  
This would insert another line between the above and the “Add more  
widgets” link. This would be able to be repeated as many times as the  
user wants to enter widgets of many different types. Also, the new  
lines should appear without refreshing the page.

Any help is appreciated. I hope this is the right forum. I figure  
this will take some AJAX. I’ve done something similar before with  
JavaScript, but it just hid/unhid a div.

Thanks,  
Ryan

---

<div class="post-metadata">

**Author:** ![ryan8720](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@ryan8720](https://www.ruby-forum.com/u/ryan8720)\
**Post date:** [October 1, 2009, 11:48am UTC](https://www.ruby-forum.com/t/generating-new-div-when-link-is-clicked/176489/2 "2009-10-01T11:48:08Z")

</div>

Hi  
You have to understand any ajax fashioned link One example

link\_to\_remote

[http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001645](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001645)

read it…Now also have a look at

insert\_html

[http://ap.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M000349](http://ap.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M000349)

And check how page.insert\_html works Examples avaible on the above link

Sijo

---

<div class="post-metadata">

**Author:** ![ryan8720](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@ryan8720](https://www.ruby-forum.com/u/ryan8720)\
**Post date:** [October 1, 2009, 3:03pm UTC](https://www.ruby-forum.com/t/generating-new-div-when-link-is-clicked/176489/3 "2009-10-01T15:03:28Z")

</div>

ryan8720 wrote:

> Hi everyone,
> 
> I’m new to rails, and I’ve got my application up and running. Now I’d  
> like to add a function that I haven’t been able to find information  
> on.
> 
> Basically, I start with a form that asks the user to input the number  
> of widgets of some type they have.
> 
> For example (\_\_\_\_ is a textfield):
> 
> \_\_\_\_\_ widgets of type \_\_\_\_\_\_.
> 
> Then I want to have a link below this that says “Add more widgets.”  
> This would insert another line between the above and the “Add more  
> widgets” link. This would be able to be repeated as many times as the  
> user wants to enter widgets of many different types. Also, the new  
> lines should appear without refreshing the page.
> 
> Any help is appreciated. I hope this is the right forum. I figure  
> this will take some AJAX. I’ve done something similar before with  
> JavaScript, but it just hid/unhid a div.

You don’t need Ajax just to add more input fields: there’s usually no  
reason to make a server request for something so simple. It can be done  
completely on the client side – look at Element.clone() .

> Thanks,  
> Ryan

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
