How to check the existence of div?

Hi…

I have an rjs file to replace a div using ajax call.

page["#{params[:id]}parents"].replace :partial =>
‘cipart/expanded_item’, :locals => { :expand_items =>
CiRelation.find_all_by_ci_id(params[:id])}

“#{params[:id]}parents” is the name of the div to be replaced.

In some cases depending on a condition, this div does not exist. Then
in the rjs file I have to check for the existence of this div. How can I
do this?

Please help.

Thanks
Suneeta

if page[“#{params[:id]}parents”]

On Tue, May 20, 2008 at 4:52 PM, Suneeta Km <
[email protected]> wrote:


Appreciated my help?
Reccommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

Ryan B. wrote:

if page[“#{params[:id]}parents”]

On Tue, May 20, 2008 at 4:52 PM, Suneeta Km <
[email protected]> wrote:


Appreciated my help?
Reccommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

Hi
Thanks for replying.

I tried this if condition. But an rjs error message is displayed.
‘Type error: $(“15parents”) has no properties’
15parents is the name of the div. 15 is obtained by evaluating
params[:id]
Please help.

Suneeta

Try suffixing it with a call to .nil?

On Tue, May 20, 2008 at 5:13 PM, Suneeta Km <
[email protected]> wrote:


Appreciated my help?
Reccommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

On 20 May 2008, at 08:44, Ryan B. (Radar) wrote:

Try suffixing it with a call to .nil?

That fundamentally cannot work. that if is a ruby if (ie evaluated
server side), but the condition is a client side one (whether a
certain element is present).

what does work is

page << “if $(‘some_id’){”
#do some other stuff
page << “}”

There is rails plugin that helps with this
http://rubyforge.org/projects/js-if-blocks/

Bear in mind that the partials are still rendered etc. no matter what.
The only thing that is conditional is whether or not you use the chunk
of html you’ve ferried over to the browser.

Fred

Frederick C. wrote:

On 20 May 2008, at 08:44, Ryan B. (Radar) wrote:

Try suffixing it with a call to .nil?

That fundamentally cannot work. that if is a ruby if (ie evaluated
server side), but the condition is a client side one (whether a
certain element is present).

what does work is

page << “if $(‘some_id’){”
#do some other stuff
page << “}”

There is rails plugin that helps with this
http://rubyforge.org/projects/js-if-blocks/

Bear in mind that the partials are still rendered etc. no matter what.
The only thing that is conditional is whether or not you use the chunk
of html you’ve ferried over to the browser.

Fred

Can I do this checking without using any plugin? I tried if
$(‘some_id’). I think the problem is, in my case the id of div is
obtained by evaluating #{params[:id]}

Ryan B. wrote:

Try suffixing it with a call to .nil?

On Tue, May 20, 2008 at 5:13 PM, Suneeta Km <
[email protected]> wrote:


Appreciated my help?
Reccommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

I tried suffixing the if with .nil? But it didn’t work.
Any other solution?

Frederick C. wrote:

On 20 May 2008, at 09:20, Suneeta Km wrote:

what does work is
The only thing that is conditional is whether or not you use the
chunk
of html you’ve ferried over to the browser.

Fred

Can I do this checking without using any plugin? I tried if
$(‘some_id’). I think the problem is, in my case the id of div is
obtained by evaluating #{params[:id]}

You don’t need the plugin, you can just use what i posted above , it’s
just a bit neeter with the plugin. if your div id is not static that
is not a problem:

page << “if $(’#{ “foo” + “bar” + “baz”}’){”

Fred

I tried the following:
page << “if $(’#{params[:id]}parents’){”
page["#{params[:id]}parents"].replace :partial =>
‘cipart/expanded_item’, :locals => { :expand_items =>
CiRelation.find_all_by_ci_id(params[:id]), :relation => ‘parents’,
:ci_id=>params[:ci_id] }
page << “}”

If div exists, the partial file is executed. But it is not replaced and
the result is not displayed.

On 20 May 2008, at 09:20, Suneeta Km wrote:

what does work is
The only thing that is conditional is whether or not you use the
chunk
of html you’ve ferried over to the browser.

Fred

Can I do this checking without using any plugin? I tried if
$(‘some_id’). I think the problem is, in my case the id of div is
obtained by evaluating #{params[:id]}

You don’t need the plugin, you can just use what i posted above , it’s
just a bit neeter with the plugin. if your div id is not static that
is not a problem:

page << “if $(’#{ “foo” + “bar” + “baz”}’){”

Fred

Frederick C. wrote:

On 20 May 2008, at 09:50, Suneeta Km wrote:

I tried the following:
page << “if $(’#{params[:id]}parents’){”

page["#{params[:id]}parents"].replace :partial =>
‘cipart/expanded_item’, :locals => { :expand_items =>
CiRelation.find_all_by_ci_id(params[:id]), :relation => ‘parents’,
:ci_id=>params[:ci_id] }
page << “}”

If div exists, the partial file is executed. But it is not replaced
and
the result is not displayed.

You’ve probably got the wrong div id. Poke around in firebug.

Fred

I printed the div id in an alert box and obtained the correct div. But
still that div is not replaced. Don’t know why.

On 20 May 2008, at 09:50, Suneeta Km wrote:

I tried the following:
page << “if $(’#{params[:id]}parents’){”

page["#{params[:id]}parents"].replace :partial =>
‘cipart/expanded_item’, :locals => { :expand_items =>
CiRelation.find_all_by_ci_id(params[:id]), :relation => ‘parents’,
:ci_id=>params[:ci_id] }
page << “}”

If div exists, the partial file is executed. But it is not replaced
and
the result is not displayed.

You’ve probably got the wrong div id. Poke around in firebug.

Fred

Frederick C. wrote:

On 20 May 2008, at 10:07, Suneeta Km wrote:

CiRelation.find_all_by_ci_id(params[:id]), :relation => ‘parents’,
But
still that div is not replaced. Don’t know why.

But have you checked that the div actually has that id? Does the
generated javscript look sane ?

Fred

Yes I checked the generated html code. The div has correct id.

On 20 May 2008, at 10:07, Suneeta Km wrote:

CiRelation.find_all_by_ci_id(params[:id]), :relation => ‘parents’,
But
still that div is not replaced. Don’t know why.

But have you checked that the div actually has that id? Does the
generated javscript look sane ?

Fred

On 20 May 2008, at 10:57, Suneeta Km wrote:

Fred

Yes I checked the generated html code. The div has correct id.
Whack a breakpoint in your javascript and follow what happens (because
you’re using rjs you’ll have to stick the breakpoint in prototype
first (evalResponse is probably a good place ))

Fred

Perhaps not for beginners but at some point in my rails development work
I realized that complex rjs files can be more trouble than they are
worth. For one, it is basically just masking javascript with ruby for no
good reason frankly and doing anything non-trivial (if statements, loops
etc) are generally easier in javascript anyways. How do you rectify this
then?

Two ways…simple way is simply write the function in pure javascript
and then invoke it in the rjs…i.e page.call “someFunction”, “param1”,
“param2”

but what if I REALLY want to be able to dynamically evaluate ruby code
as part of the template…I have used a plugin called MinusMOR

http://agilewebdevelopment.com/plugins/minusmor

Basically allows you to write javascript and embed ruby much the same
way as writing html and embedding ruby in erb.

Anyways, just something to think about. Javascript is not evil (in my
opinion) and often it is actually easier just to bite the bullet and use
it directly.

Frederick C. wrote:

On 20 May 2008, at 10:07, Suneeta Km wrote:

Frederick C. wrote:

On 20 May 2008, at 09:50, Suneeta Km wrote:

I tried the following:
page << “if $(’#{params[:id]}parents’){”

If you’d checked the generated javascript you would have seen that
there’s an obvious syntax error here. It should be
page << “if($(’#{params[:id]}parents’)){”

Fred

Thank you so much Fred. It was a syntax error.
I gave page << “if($(’#{params[:id]}parents’)){” and it worked.
Thanks
Suneeta

On 20 May 2008, at 10:07, Suneeta Km wrote:

Frederick C. wrote:

On 20 May 2008, at 09:50, Suneeta Km wrote:

I tried the following:
page << “if $(’#{params[:id]}parents’){”

If you’d checked the generated javascript you would have seen that
there’s an obvious syntax error here. It should be
page << “if($(’#{params[:id]}parents’)){”

Fred