# Setting the Element ID of inserted partials with javascript

**URL:** <https://www.ruby-forum.com/t/setting-the-element-id-of-inserted-partials-with-javascript/132115>\
**Category:** Rails\
**Created:** [March 17, 2008, 5:38am UTC](https://www.ruby-forum.com/t/setting-the-element-id-of-inserted-partials-with-javascript/132115 "2008-03-17T05:38:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_Cool\_Web\_Guy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbe5ce/32.png) [@The\_Cool\_Web\_Guy](https://www.ruby-forum.com/u/The_Cool_Web_Guy)\
**Post date:** [March 17, 2008, 5:38am UTC](https://www.ruby-forum.com/t/setting-the-element-id-of-inserted-partials-with-javascript/132115/1 "2008-03-17T05:38:06Z")

</div>

Hi,

Lets say I have a form with multiple task elements, for adding tasks  
to a project, and a link\_to\_function which inserts a new task using  
page.insert\_html :partial  
eg:

tasks holder
\<%= link\_to\_function "Add Task" do |page| page.insert\_html :bottom, :tasks, :partial =\> 'task', :object =\> @task.new end %\>

I know when I do a render collection that each partial has a  
partial\_name\_counter variable eg: task\_counter, but when I create a  
task using the javascript helper this variable no longer works because  
it defaults to 1 (as it was when the partial was rendered into the  
javascript query), is there anyway to get the same attribute using  
javascript?, or at least set a unique id to each task which is created  
using the javascript helper?

Thanks in advance.  
-Ivan

---

<div class="post-metadata">

**Author:** ![The\_Cool\_Web\_Guy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbe5ce/32.png) [@The\_Cool\_Web\_Guy](https://www.ruby-forum.com/u/The_Cool_Web_Guy)\
**Post date:** [March 17, 2008, 7:21am UTC](https://www.ruby-forum.com/t/setting-the-element-id-of-inserted-partials-with-javascript/132115/2 "2008-03-17T07:21:51Z")

</div>

If you want that ID in order to remove the newly added task, you can  
use Prototype’s up function. Please take a look at  
[#74 Complex Forms Part 2 - RailsCasts](http://railscasts.com/episodes/74).  
Not sure if this is what you are trying to do.

---

<div class="post-metadata">

**Author:** ![The\_Cool\_Web\_Guy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbe5ce/32.png) [@The\_Cool\_Web\_Guy](https://www.ruby-forum.com/u/The_Cool_Web_Guy)\
**Post date:** [March 17, 2008, 11:37am UTC](https://www.ruby-forum.com/t/setting-the-element-id-of-inserted-partials-with-javascript/132115/3 "2008-03-17T11:37:41Z")

</div>

Thats a good idea I’ll check it out,

What i am actually doing is using the spry framework to provide  
validation on the textfield and unfortunately it has to be applied on  
a per id basis, not on all elements of a certain class, so therefor i  
need to be able to set a unique id to each task input field or  
containing div (as spry can be applied to a div then auto assign to  
first input)
