RJS page.select call chaining

I’ve run into an annoying thing about the Javascript generated by an RJS
template of mine and I’m not sure how to work around it. I’d prefer to
avoid writing raw JS if I can. I’m looping through a set of elements and
removing four different CSS classes, only one of which will be present
for each element but I don’t know which one.

page.select(’#content div.address_box’).each do |elem|
elem.removeClassName ‘foreign_outline’
elem.removeClassName ‘good_outline’
elem.removeClassName ‘normal_outline’
elem.removeClassName ‘bad_outline’
end

According to the error I get, the JS produced by this actually chains
all of those removeClassName calls together, because each one is
supposed to return the node from which the class was removed.

$$("#content div.address_box").each(function(value, index){
value.removeClassName(“foreign_outline”).removeClassName(“good_outline”)…etc.

If any of the removeClassName calls fails it returns nil and the next
call in the chain throws an error. If there isn’t a way to force these
calls to not be chained then I guess I’ll be doing raw JS. Any ideas?

Instead of doing the whole thing in raw Javascript, you could do:

page.select(’#content div.address_box’).each do |elem|
page << “Element.removeClassName(value, ‘foreign_outline’)”
page << “Element.removeClassName(value, ‘good_outline’)”
etc
end

That’s a nice compromise that didn’t occur to me. Thanks!