RJS - checking if element exists?

Hiya,

I’m trying to get around the problem of not knowing whether to use
insert_html or replace_html as there will be times when I will need to
add new elements or replace existing ones. Is there some form of check
to see if a DOM element exists on the page? Or can I catch errors so
that I know there isn’t an element of a certain name already there?

Hope that makes sense,
Bex

You (obviously) can’t do this purely server side, you can however as
part of your rjs generate the necessary javascript to test for this.
So, in fugly land you can do this

page<< “if($(‘element_id’)){”
page[‘element_id’].hide]’
page << “}”

However this is a plugin
http://www.agilewebdevelopment.com/plugins/rjs_if_unless_blocks that
allows you to instead do

page.if page[element_id].visible do
page[element_id].hide
end

Fred

On 8 November 2006 14:27, Becky F. wrote:

I’m trying to get around the problem of not knowing whether to use
insert_html or replace_html as there will be times when I will need to
add new elements or replace existing ones. Is there some form of check
to see if a DOM element exists on the page? Or can I catch errors so
that I know there isn’t an element of a certain name already there?
There are at least two solutions:

  1. Create custom javascript methods, e.g.
    function create_or_update_element(id, content) {

    }

  2. Install js-if-blocks plugin
    ./script/plugin install
    svn://rubyforge.org/var/svn/js-if-blocks/trunk/js-if-blocks

and then write your RJS code like this:

page.if page[my_element_id] do
page.replace_html element_id, content
page.else
page.insert_html :top, parent_id,

#{content}

end

Thanks so much for the replies. Am I right in thinking that these
plugins will work if the element itself isn’t there? I tried:

page.if page[‘test’] do
puts “Working!”
page.else
puts “Else working!”
end

Where test does not exist on the page, and I get an error saying If is
not defined :os

Thanks again for the help!
Bex

Where test does not exist on the page, and I get an error saying If is
not defined :os
As far as I understand, you haven’t installed plugin yet. The error is
because “page.if” method is defined in that plugin and not part of
standard
Rails distribution.

Ah I see. I’m working on windows and I wasn’t sure if the plugin had
installed or not, as I have no error message when i (from the root of my
app) type: ruby script/plugin install
svn://rubyforge.org/var/svn/js-if-blocks/trunk/js-if-blocks

I can just see it being something stupid I’m doing wrong, but I’m
assuming that doesn’t install the plugin?

Thanks again!
Bex

On 8 November 2006 16:37, Becky F. wrote:

Thanks so much for the replies. Am I right in thinking that these
plugins will work if the element itself isn’t there?

  1. Actually, those two references are to the same plugin
  2. Yes, the condition will work.

I tried:

page.if page[‘test’] do
puts “Working!”
page.else
puts “Else working!”
end

“puts” will not work, because, actually, there is no true “if”
statement.
Those are just methods that generate proper javascript. So, in your
case, you
should use something like page.alert(‘Working!’)

Where test does not exist on the page, and I get an error saying If is
not defined :os
As far as I understand, you haven’t installed plugin yet. The error is
because “page.if” method is defined in that plugin and not part of
standard
Rails distribution.

On 8 November 2006 18:17, Becky F. wrote:

I can just see it being something stupid I’m doing wrong, but I’m
assuming that doesn’t install the plugin?
You can check if you have non-empty js-if-blocks directory
under ./vendor/plugins directory.

Also, make sure you have restarted you server.

Maxim K. wrote:

On 8 November 2006 18:17, Becky F. wrote:

I can just see it being something stupid I’m doing wrong, but I’m
assuming that doesn’t install the plugin?
You can check if you have non-empty js-if-blocks directory
under ./vendor/plugins directory.

Also, make sure you have restarted you server.

Honestly you have completely solved a massive problem with this plugin!
Turned out I had the rails versioning wrong or something so once I fixed
that the plugin installed and worked like a dream! Now back to
officially loing RJS :smiley:

Thanks sooooo much!
Bex

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs