Topic: How to check if DOM ID exists?


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 only return a reference you can use in the JS..

I tried this:
    page.insert_html ...

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


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