Link_to_function check javascript attribute

How could I check the value of an javascript object’s attribute?

<%= link_to_function(“Show details…”, nil, :id => “show_details”) do
|page|
page.visual_effect(:toggle_blind, :new_item_details)

      if (page[:new_item_details].style.visibility == "none")

<----------------------------------------???
page[:show_details].replace_html “Hide details”
else
page[:show_details].replace_html “Show details”
end

    end %>

It tries to call style().visibility().== and this throws a javascript
error

On Aug 24, 10:48 pm, Eduardo Yáñez Parareda [email protected]
wrote:

      else
        page[:show_details].replace_html "Show details"
      end

    end %>

short answer: you can’t use a ruby if. you need to generate the
appropriate javascript yourself.
long answer:

Fred

Thanks Frederick!, the long answer is what I needed :), very useful
indeed.
I also tried to write a RJS partial embedding javascript, then render
it within the view, but
I only got an HTML page not rendered.

In the end, what I want to do is toggle a div, and replace an element
HTML with some content when the div is hidden, and with another
content when the div is showed:

Initially, the div is hidden with style=display:none

<%= link_to_function("Show details...", nil, :id =>

“show_details”) do |page|
page.visual_effect :toggle_blind, :new_item_details
if page[:new_item_details].visible
page[:show_details].replace_html “Hide details”
else
page[:show_details].replace_html “Show details”
end
end %>

so when user clicks the link, text is changed from ‘Show details’ to
‘Hide details’, but I never get to change ‘Hide details’ to ‘Show
details’, it seems that ‘new_item_details’ div
is always visible when it isn’t.

On 25 ago, 18:08, Eduardo Yáñez Parareda [email protected]

On 25 Aug 2008, at 18:44, Eduardo Yáñez Parareda wrote:

page << “else”
text, but no errors. What am i doing wrong?
You’re creating syntactically invalid javascript: the curly brace
you’re opening on the if line is never closed.

Fred

Thanks again, but that’s not the problem, because if I fix the syntax
error or leave the partial RJS file empty.
My fixed partial is:

page << “function toggle_details() {”
page.visual_effect :toggle_blind, ‘new_item_details’
page << “if ($(‘new_item_details’).style.display == ‘none’) {”
page[‘show_details’].replace_html “Hide details”
page << “} else {”
page[‘show_details’].replace_html “Show details”
page << “}”
page << “}”

but the page is not rendered fine. So I’ve chosen an easy solution,
I’ve written the function into application.js as javascript, and all
works fine now.
Thank you Frederick for your help!

Well, as I realized I need to generate the javascript by myself, I’ve
written a RJS partial:

_toggle_details.rjs

page << “function toggle_details() {”
page.visual_effect :toggle_blind, ‘new_item_details’
page << “if ($(‘new_item_details’).style.display == ‘none’) {”
page[‘show_details’].replace_html “Hide details”
page << “else”
page[‘show_details’].replace_html “Show details”
page << “}”

then in my view (which is an .erb partial) I render it:

But I don’t get the page rendered fine, I only get the HTML as plain
text, but no errors. What am i doing wrong?

On 25 ago, 18:08, Eduardo Yáñez Parareda [email protected]

On 25 Aug 2008, at 21:39, Eduardo Yáñez Parareda wrote:

page[‘show_details’].replace_html “Show details”
page << “}”
page << “}”

but the page is not rendered fine. So I’ve chosen an easy solution,
I’ve written the function into application.js as javascript, and all
works fine now.

Hard to say without seeing what was inserted into the page, but if the
javascript that is created included " (which it probably does) then
crazy stuff would happen. Wrapping the render :partial in a
javascript_tag would probably have helped.

Fred

Yes, may be that double quotes broke the page. Anyway it’s more clear
to have javascript in a javascript file :), but I was trying to do it
with RJS because
I’m not used to use it.

Although it already works fine into application.js, I’ve tried one
last attempt with RJS. My partial now is:

page << ‘function toggle_details() {’
page.visual_effect :toggle_blind, ‘new_item_details’
page << “if ($(‘new_item_details’).style.display == ‘none’) {”
page[‘show_details’].replace_html ‘Hide details’
page << ‘} else {’
page[‘show_details’].replace_html ‘Show details’
page << ‘} }’

And the generated code is:

but still doesn’t render fine. Moreover, if I leave the partial empty
(nothing into the RJS), it doesn’t work!!!

On 25 ago, 23:57, Eduardo Yáñez Parareda [email protected]

On 25 Aug 2008, at 23:13, Eduardo Yáñez Parareda wrote:

Although it already works fine into application.js, I’ve tried one
last attempt with RJS. My partial now is:

Forgot to say - I think this is absolutely the right way to go -
there’s no point trying to shoehorn stuff into rjs which is more
easily written as just plain javascript. But it’s always good to
understand why something didn’t work.

Fred

On 25 Aug 2008, at 23:13, Eduardo Yáñez Parareda wrote:

page << ‘} }’
$(“show_details”).update(“Show details”);
}
}’);
throw e }

but still doesn’t render fine. Moreover, if I leave the partial empty
(nothing into the RJS), it doesn’t work!!!

because of the try/catch stuff that’s still there even if the partial
is empty.
The javascript_tag helper will wrap this in a CDATA section which
should make everything happy.

Fred