count <li> items in div, RJS

Hi Igor,

Igor K. wrote:

How i can count <li> items in div using RJS?

<div id='results'>
  <li id="1"> one </li>
  <li id="2"> two </li>
  <li id="3"> three </li>
</div>
<%= link_to_remote.......ADD %>

and in add action i want to add new li item with
unique ID and get <li>'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

Hi Igor,

Igor K. wrote:

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', "<li id=#{count.to_s}>#{a.to_s + "
>>> " + count.to_s}</li>"
page.visual_effect :highlight, count.to_s, :duration => 2.5

and following in view
<div id="results">
  <li>one</li>
</div>
<%= 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

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
  • in your layout you will have the number of li’s.

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

    cheers
    Ivor