# How do I generate dynamic items dynamically?

**URL:** <https://www.ruby-forum.com/t/how-do-i-generate-dynamic-items-dynamically/171555>\
**Category:** Rails\
**Created:** [July 5, 2009, 6:01am UTC](https://www.ruby-forum.com/t/how-do-i-generate-dynamic-items-dynamically/171555 "2009-07-05T06:01:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 5, 2009, 6:01am UTC](https://www.ruby-forum.com/t/how-do-i-generate-dynamic-items-dynamically/171555/1 "2009-07-05T06:01:11Z")

</div>

Hi All,

I’m looking for a way to append elements with javascript using  
javascript. Basically I have two lists of items. One has a list of  
items with an “ADD” button (using “link\_to\_remote”) and the other has  
a list of items with a “REMOVE” button (using “link\_to\_remote”). When  
I click the “ADD” it immediately places the item into the other list.  
However, I need to be able to have the newly inserted item perform the  
reverse action for “REMOVE”.

I see no way (besides, perhaps creating a partial to render a non-  
object) to dynamically generate dynamic items of this nature.

Any ideas?

Best.

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 5, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/how-do-i-generate-dynamic-items-dynamically/171555/2 "2009-07-05T19:32:06Z")

</div>

Michael Williams wrote:

> Hi All,
> 
> I’m looking for a way to append elements with javascript using  
> javascript. Basically I have two lists of items. One has a list of  
> items with an “ADD” button (using “link\_to\_remote”) and the other has  
> a list of items with a “REMOVE” button (using “link\_to\_remote”).

Do you really need to do it that way? That will call the server every  
time a button is clicked. You might want to do the list editing on the  
client side, then have a Save button that sends the list to the server.

> When  
> I click the “ADD” it immediately places the item into the other list.  
> However, I need to be able to have the newly inserted item perform the  
> reverse action for “REMOVE”.

So the Add button works, but the Remove button does not? You should be  
able to do both the same way – after all, the only difference is that  
Add moves things from list 1 to list 2, while Remove does likewise from  
list 2 to list 1.

> I see no way (besides, perhaps creating a partial to render a non-  
> object) to dynamically generate dynamic items of this nature.

I am not sure what your problem is. Please provide more detail and  
code.

> Any ideas?
> 
> Best.

## Best,

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

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 6, 2009, 1:23am UTC](https://www.ruby-forum.com/t/how-do-i-generate-dynamic-items-dynamically/171555/3 "2009-07-06T01:23:45Z")

</div>

> I am not sure what your problem is. Please provide more detail and  
> code.

Basically I have two partials that render. One is the “Available  
Items” and the other is “Selected Items”. If I select an “Available  
Item” then it gets added to “Selected Items”. However, if I remove a  
“Selected Item” I need it to go back to “Available Items”. When they  
are initially rendered from the VIEW code, both work just fine.  
However, I need to dynamically insert javascript for each newly added  
unordered list item to either column. Currently I’m actually using a  
partial within a partial to manage each new item. It adds a  
“link\_to\_remote” item for each newly added item. My “issue” is that  
I can’t figure out how to use the javascript helpers via the  
“render :update” method in the controller so I have to call a partial  
in order to insert the appropriate code. I could very likely be  
asking the wrong question(s)

The reason I need it to update immediately is because it is a multi-  
user setup and if one user has selected an item, I need it to  
immediately be “taken out” of circulation. In either case, I’d still  
need dynamic code to, at the very least, move the items back and  
forth.

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 6, 2009, 6:42am UTC](https://www.ruby-forum.com/t/how-do-i-generate-dynamic-items-dynamically/171555/4 "2009-07-06T06:42:35Z")

</div>

Michael Williams wrote:

> > I am not sure what your problem is. ï¿½Please provide more detail and  
> > code.
> 
> Basically I have two partials that render. One is the “Available  
> Items” and the other is “Selected Items”. If I select an “Available  
> Item” then it gets added to “Selected Items”. However, if I remove a  
> “Selected Item” I need it to go back to “Available Items”.

Yes, I get that. As I said in my first message, if you can do one, you  
can do the other the same way – so what’s the problem?

> When they  
> are initially rendered from the VIEW code, both work just fine.  
> However, I need to dynamically insert javascript for each newly added  
> unordered list item to either column.

By “dynamically insert javascript”, do you mean that you think you need  
to write appropriate JS to the onclick attribute in the HTML tag? If  
so, that’s incorrect. You just need to add behavior to the appropriate  
DOM element, and that’s very easy to do.

> Currently I’m actually using a  
> partial within a partial to manage each new item. It adds a  
> “link\_to\_remote” item for each newly added item.

Why two levels of partials? I don’t see how that’s necessary here.

> My “issue” is that  
> I can’t figure out how to use the javascript helpers via the  
> “render :update” method in the controller so I have to call a partial  
> in order to insert the appropriate code.

As I keep saying on this list, _learn_ the framework instead of kludging  
your way around it.

> I could very likely be  
> asking the wrong question(s)

It seems to me that you should be asking how to use render :update (or  
similar) properly. Or just don’t use RJS and write the JavaScript  
directly.

> The reason I need it to update immediately is because it is a multi-  
> user setup and if one user has selected an item, I need it to  
> immediately be “taken out” of circulation.

I understand. I’ve done things like that as well, though not with  
Rails.

> In either case, I’d still  
> need dynamic code to, at the very least, move the items back and  
> forth.

I do not know if you really know what you mean when you say “dynamic  
code”. Please explain how you understand that phrase; I think you may  
be using it in an inaccurate sense.

## Best,

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