Topic: How to check if DOM ID exists?

Hi,

Does anyone know how to check in a .rjs file if a given
DOM ID already exists (and create it if not)?

page['id'] and page.select only return a reference you can use in the JS..

I tried this:
if page.select('ul#id').empty?
    page.insert_html ...
end


but that doesn't seem to work, it inserts $$("ul#id").empty?(); and further
calls aren't executed.


thanks
Christoph

Re: How to check if DOM ID exists?

The "if" condition is evaluated in Ruby, but you have to do the evaluation in Javascript - so you need to do "if" there. Something like this (roughly)

page << "if ($('some_id')) {"
page.alert "id exists"
page << "} else {"
page.alert "id doesn't exists"
page << "}"

Of course you can replace the alerts with whatever you want. Untested.

Railscasts - Free Ruby on Rails Screencasts

Re: How to check if DOM ID exists?

perfect, thanks smile