Topic: number of elements of a class with RJS

i need to know how many spans on my page have the class 'commentHead'

none of these work

in RJS file

  @comment_counter = 1
  page.select('span.commentHead').each do |value|
   @comment_counter += 1
  end
 
#OR
@comment_counter = page.select('span.commentHead').length

Re: number of elements of a class with RJS

The select statement behaves a little strangely. it isn't actually looping through the found elements in Ruby, instead it is generating the javascript to do the looping. This is because Ruby does not have access to the HTML of the page. In other words, the loop doesn't happen until after the Ruby is executed and the Javascript is sent back to the client.

What specifically are you trying to do? Perhaps it can be done in JavaScript.

Railscasts - Free Ruby on Rails Screencasts

Re: number of elements of a class with RJS

i'm adding new comments via AJAX but instead of running a new query and rendering a new partial collection, im just appending the new comment to the bottom of the comments section, and i need to know how many are already there for my alternating rows CSS class.

looks like i'll have to page.call a custom function to figure out what row class to use.

i just hate having the command routed through so many different sources. i'm sure when i come back to this in 2 months, it'll take me 3 hours to figure out how the hell the comment is "magically" appearing at the end of the comments section.