# Count \<li\> items in div, RJS

**URL:** <https://www.ruby-forum.com/t/count-li-items-in-div-rjs/115172>\
**Category:** Rails\
**Created:** [September 21, 2007, 11:25am UTC](https://www.ruby-forum.com/t/count-li-items-in-div-rjs/115172 "2007-09-21T11:25:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![demoversion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@demoversion](https://www.ruby-forum.com/u/demoversion)\
**Post date:** [September 21, 2007, 11:25am UTC](https://www.ruby-forum.com/t/count-li-items-in-div-rjs/115172/1 "2007-09-21T11:25:30Z")

</div>

Hello,

How i can count

- items in div using RJS?

- one 
- two 
- three 
\<%= link\_to\_remote.......ADD %\>

and in add action i want to add new li item with unique ID and get

- Thanks

---

<div class="post-metadata">

**Author:** ![demoversion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@demoversion](https://www.ruby-forum.com/u/demoversion)\
**Post date:** [September 21, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/count-li-items-in-div-rjs/115172/2 "2007-09-21T12:45:28Z")

</div>

Hi Igor,

Igor K. wrote:

> unique ID and get
> 
> - 's elements count?  
> Just count them in the controller method you invoke as you add them.  
> Store  
> the count in a session variable or in a table to persist your count.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![demoversion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@demoversion](https://www.ruby-forum.com/u/demoversion)\
**Post date:** [September 21, 2007, 1:38pm UTC](https://www.ruby-forum.com/t/count-li-items-in-div-rjs/115172/3 "2007-09-21T13:38:18Z")

</div>

> > unique ID and get
> > 
> > - 's elements count?  
> > Just count them in the controller method you invoke as you add them.  
> > Store  
> > the count in a session variable or in a table to persist your count.

I have a following code in add.rjs

count = 0  
page.select(’#results li’).each do |element|  
count = count + 1  
#element.hide  
end  
page.insert\_html :bottom, ‘results’, "

- #{a.to\_s + "

> > > " + count.to\_s}

"  
page.visual\_effect :highlight, count.to\_s, :duration =\> 2.5

and following in view

- one

\<%= link\_to\_remote "add", :url =\> {:action =\> :add} %\>

and i always get count = 1

---

<div class="post-metadata">

**Author:** ![demoversion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@demoversion](https://www.ruby-forum.com/u/demoversion)\
**Post date:** [September 22, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/count-li-items-in-div-rjs/115172/4 "2007-09-22T16:30:24Z")

</div>

Hi Igor,

Igor K. wrote:

> and following in view
> 
> - one
> 
> \<%= link\_to\_remote "add", :url =\> {:action =\> :add} %\>
> 
> and i always get count = 1

I’m not sure I understand exactly what it is you’re trying to  
accomplish,  
but in general… I would recomment keeping track of ‘count’ in your  
controller method; storing / retrieving it there and making it available  
to  
your view via an instance variable. You’re not dealing with a static  
page  
of which you have no knowledge. Everything that’s on the page is put  
there  
by your app during the session. No?

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![demoversion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@demoversion](https://www.ruby-forum.com/u/demoversion)\
**Post date:** [September 22, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/count-li-items-in-div-rjs/115172/5 "2007-09-22T23:09:42Z")

</div>

would recomment keeping track of ‘count’ in your

> controller method; storing / retrieving it there and making it available  
> to  
> your view via an instance variable. You’re not dealing with a static  
> page

Yes, if i work with dynamic pages i can save elements count to session,  
but my question is connected with static pages.  
For example designer add one element to div, and i set count variable in  
controller to 1, but when designed add new element to div, again i need  
to change count variable.  
i just want to know is it possible count some elements, for example

- by id of 
 where are they located.

maybe anybody can give a advice how to set id of new element to unique  
value(i guess i can user time(in milliseconds) for example)

Thanks

---

<div class="post-metadata">

**Author:** ![demoversion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@demoversion](https://www.ruby-forum.com/u/demoversion)\
**Post date:** [September 23, 2007, 12:29pm UTC](https://www.ruby-forum.com/t/count-li-items-in-div-rjs/115172/6 "2007-09-23T12:29:10Z")

</div>

Hi Igor

In an action in the controller, responding to the xhr request when you  
do

render :update to |page|

I just did a page.inspect in the log and found @content\_for\_layout - I  
think  
you would have to do something like page.session.content\_for\_layout -  
the  
log is pretty ugly - you can check.

But, if you have @content\_for\_layout you could count the occurance of

- in that and unless you have 
- However, this sounds very convoluted - you would probably be better of  
just  
adding the milliseconds id for the li.
