# link\_to\_function check javascript attribute

**URL:** <https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582>\
**Category:** rubyonrails-talk\
**Created:** [August 24, 2008, 9:48pm UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582 "2008-08-24T21:48:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eduardo\_Yanez\_Parare](https://avatars.discourse-cdn.com/v4/letter/e/e56c9b/32.png) [@Eduardo\_Yanez\_Parare](https://discuss.rubyonrails.org/u/Eduardo_Yanez_Parare)\
**Post date:** [August 24, 2008, 9:48pm UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582/1 "2008-08-24T21:48:47Z")

</div>

How could I check the value of an javascript object's attribute? &nbsp;&nbsp;&nbsp;&nbsp;\<div id="new\_item\_details" style="display:none"\>\</div\>

\<%= link\_to\_function("Show details...", nil, :id =\> "show\_details") do

> page\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page.visual\_effect(:toggle\_blind, :new\_item\_details)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (page[:new\_item\_details].style.visibility == "none") \<----------------------------------------??? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page[:show\_details].replace\_html "Hide details" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page[:show\_details].replace\_html "Show details" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end %\>

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

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 25, 2008, 8:57am UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582/2 "2008-08-25T08:57:13Z")

</div>

short answer: you can't use a ruby if. you need to generate the appropriate javascript yourself. long answer: [Conditional RJS explained - Space Vatican](http://www.spacevatican.org/2008/5/26/conditional-rjs-explained)

Fred

---

<div class="post-metadata">

**Author:** ![Eduardo\_Yanez\_Parare](https://avatars.discourse-cdn.com/v4/letter/e/e56c9b/32.png) [@Eduardo\_Yanez\_Parare](https://discuss.rubyonrails.org/u/Eduardo_Yanez_Parare)\
**Post date:** [August 25, 2008, 4:08pm UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582/3 "2008-08-25T16:08:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Eduardo\_Yanez\_Parare](https://avatars.discourse-cdn.com/v4/letter/e/e56c9b/32.png) [@Eduardo\_Yanez\_Parare](https://discuss.rubyonrails.org/u/Eduardo_Yanez_Parare)\
**Post date:** [August 25, 2008, 5:44pm UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582/4 "2008-08-25T17:44:12Z")

</div>

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:

\<script type="text/javascript"\> &nbsp;&nbsp;\<%= render :partial =\> 'toggle\_details', :type =\> 'rjs' %\> \</script\>

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?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 25, 2008, 5:51pm UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582/5 "2008-08-25T17:51:00Z")

</div>

> 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:
> 
> \<script type="text/javascript"\> \<%= render :partial =\> 'toggle\_details', :type =\> 'rjs' %\> \</script\>
> 
> 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?

You're creating syntactically invalid javascript: the curly brace  
you're opening on the if line is never closed.

Fred

---

<div class="post-metadata">

**Author:** ![Eduardo\_Yanez\_Parare](https://avatars.discourse-cdn.com/v4/letter/e/e56c9b/32.png) [@Eduardo\_Yanez\_Parare](https://discuss.rubyonrails.org/u/Eduardo_Yanez_Parare)\
**Post date:** [August 25, 2008, 8:39pm UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582/6 "2008-08-25T20:39:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 25, 2008, 8:52pm UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582/7 "2008-08-25T20:52:17Z")

</div>

> 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.

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

---

<div class="post-metadata">

**Author:** ![Eduardo\_Yanez\_Parare](https://avatars.discourse-cdn.com/v4/letter/e/e56c9b/32.png) [@Eduardo\_Yanez\_Parare](https://discuss.rubyonrails.org/u/Eduardo_Yanez_Parare)\
**Post date:** [August 25, 2008, 9:57pm UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582/8 "2008-08-25T21:57:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Eduardo\_Yanez\_Parare](https://avatars.discourse-cdn.com/v4/letter/e/e56c9b/32.png) [@Eduardo\_Yanez\_Parare](https://discuss.rubyonrails.org/u/Eduardo_Yanez_Parare)\
**Post date:** [August 25, 2008, 10:13pm UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582/9 "2008-08-25T22:13:04Z")

</div>

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:

\<script type='text/javascript'\> &nbsp;&nbsp;try { &nbsp;&nbsp;&nbsp;&nbsp;function toggle\_details() { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Effect.toggle("new\_item\_details",'blind',{}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($('new\_item\_details').style.display == 'none') { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$("show\_details").update("Hide details"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$("show\_details").update("Show details"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} catch (e) { &nbsp;&nbsp;&nbsp;&nbsp;alert('RJS error:\n\n' + e.toString()); &nbsp;&nbsp;&nbsp;&nbsp;alert('function toggle\_details() {\n &nbsp;&nbsp;&nbsp;&nbsp;Effect.toggle(\"new\_item\_details\",\'blind\',{});\n &nbsp;&nbsp;&nbsp;&nbsp;if ($(\'new\_item\_details\').style.display == \'none\') {\n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(\"show\_details\").update(\"Hide details\");\n &nbsp;&nbsp;&nbsp;&nbsp;} else {\n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(\"show\_details\").update(\"Show details\");\n &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}'); &nbsp;&nbsp;throw e } \</script\>

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

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 25, 2008, 10:29pm UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582/10 "2008-08-25T22:29:44Z")

</div>

> 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:
> 
> \<script type='text/javascript'\> try { &nbsp;&nbsp;&nbsp;function toggle\_details() { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Effect.toggle("new\_item\_details",'blind',{}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($('new\_item\_details').style.display == 'none') { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$("show\_details").update("Hide details"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$("show\_details").update("Show details"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;} } catch (e) { &nbsp;&nbsp;&nbsp;alert('RJS error:\n\n' + e.toString()); &nbsp;&nbsp;&nbsp;alert('function toggle\_details() {\n &nbsp;&nbsp;&nbsp;Effect.toggle(\"new\_item\_details\",\'blind\',{});\n &nbsp;&nbsp;&nbsp;if ($(\'new\_item\_details\').style.display == \'none\') {\n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(\"show\_details\").update(\"Hide details\");\n &nbsp;&nbsp;&nbsp;} else {\n &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(\"show\_details\").update(\"Show details\");\n &nbsp;&nbsp;&nbsp;} }'); throw e } \</script\>
> 
> 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

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 25, 2008, 10:31pm UTC](https://discuss.rubyonrails.org/t/link-to-function-check-javascript-attribute/33582/11 "2008-08-25T22:31:01Z")

</div>

> 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
