# Adding conditions (if-else) to RJS template

**URL:** <https://www.ruby-forum.com/t/adding-conditions-if-else-to-rjs-template/158089>\
**Category:** Rails\
**Created:** [January 19, 2009, 9:27am UTC](https://www.ruby-forum.com/t/adding-conditions-if-else-to-rjs-template/158089 "2009-01-19T09:27:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [January 19, 2009, 9:27am UTC](https://www.ruby-forum.com/t/adding-conditions-if-else-to-rjs-template/158089/1 "2009-01-19T09:27:28Z")

</div>

I tried following Ryan B.’ railscast on rjs tips  
([#45 RJS Tips - RailsCasts](http://railscasts.com/episodes/45-rjs-tips)) and apply his method of  
using if-else inside the rjs template, but can’t seem to get it to work.  
Basically, I’m trying to toggle the text on a link when it’s clicked  
(test.js.rjs):

page \<\< “if ($(‘test\_link’).value == ‘one’ {”  
page.replace\_html ‘test\_link’, ‘two’  
page \<\< “} else {”  
page.replace\_html ‘test\_link’, ‘one’  
page \<\< “}”

I believe I have the necessary view and controller code:

\<%= link\_to\_remote ‘one’, :url =\> { :action =\> ‘test’ } %\>

and

def test  
respond\_to do |format|  
format.js  
end  
end

---

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [January 19, 2009, 9:56am UTC](https://www.ruby-forum.com/t/adding-conditions-if-else-to-rjs-template/158089/2 "2009-01-19T09:56:55Z")

</div>

On 19 Jan 2009, at 09:27, Shilo A. wrote:

> page \<\< “} else {”  
> page.replace\_html ‘test\_link’, ‘one’  
> page \<\< “}”

Syntax error - does not compute.

Look closely and see how you forgot the “)” after “‘one’”. Javascript  
is very punctuation sensitive, you need to pay special attention to  
it. Also, use Firefox for testing and install Firebug to debug your  
javascript. It would have indicated this error.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [January 19, 2009, 10:37am UTC](https://www.ruby-forum.com/t/adding-conditions-if-else-to-rjs-template/158089/3 "2009-01-19T10:37:23Z")

</div>

> Syntax error - does not compute.
> 
> Look closely and see how you forgot the “)” after “‘one’”. Javascript  
> is very punctuation sensitive, you need to pay special attention to  
> it. Also, use Firefox for testing and install Firebug to debug your  
> javascript. It would have indicated this error.

Thanks Peter. I changed my code to include the closing parenthesis, but  
it seems to not work still. I am using firefox w/ firebug, although I’m  
still not that seasoned in debugging js - I’ll read up on that. Here’s  
what I have now:

page \<\< “if ($(‘test\_link’).value == ‘one’) {”  
page.alert(‘hello!’)  
page \<\< “}”

---

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [January 19, 2009, 11:48am UTC](https://www.ruby-forum.com/t/adding-conditions-if-else-to-rjs-template/158089/4 "2009-01-19T11:48:35Z")

</div>

On 19 Jan 2009, at 09:37, Shilo A. wrote:

> but  
> it seems to not work still. I am using firefox w/ firebug, although  
> I’m  
> still not that seasoned in debugging js - I’ll read up on that. Here’s  
> what I have now:

Do links even have a value attribute? It doesn’t seem to contain the  
links text if that’s what you were expecting.

Fred

---

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [January 19, 2009, 11:47am UTC](https://www.ruby-forum.com/t/adding-conditions-if-else-to-rjs-template/158089/5 "2009-01-19T11:47:25Z")

</div>

On 19 Jan 2009, at 10:37, Shilo A. wrote:

> Thanks Peter. I changed my code to include the closing parenthesis,  
> but  
> it seems to not work still. I am using firefox w/ firebug, although  
> I’m  
> still not that seasoned in debugging js - I’ll read up on that. Here’s  
> what I have now:
> 
> page \<\< “if ($(‘test\_link’).value == ‘one’) {”  
> page.alert(‘hello!’)  
> page \<\< “}”

Then add before your first “page \<\<” statement:

page \<\< “console.log($(‘test\_link’));”  
page \<\< “console.log($(‘test\_link’).value);”

There must be something wrong with the value you are trying to access  
then.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [January 19, 2009, 1:10pm UTC](https://www.ruby-forum.com/t/adding-conditions-if-else-to-rjs-template/158089/6 "2009-01-19T13:10:58Z")

</div>

> Do links even have a value attribute? It doesn’t seem to contain the  
> links text if that’s what you were expecting.
> 
> Fred

Fred - I tried using $(‘test\_link’).innerHtml == ‘one’, but the result  
is the same.

Peter - Using firebug, I tried typing in the console the following:

> > console.log($(‘test\_link’).value)  
> > undefined
> 
> > console.log($(‘test\_link’).innerHtml)  
> > undefined

I tried just using a simple span hello to see if  
console.log works, and I get the same ‘undefined’ message, even though  
it’s showing in the page markup.

I don’t want to make this an issue and take up anymore of your time – I  
guess I need to improve on my javascript.

Thanks for all your help!

---

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [January 19, 2009, 1:32pm UTC](https://www.ruby-forum.com/t/adding-conditions-if-else-to-rjs-template/158089/7 "2009-01-19T13:32:26Z")

</div>

On 19 Jan 2009, at 12:10, Shilo A. wrote:

> > Do links even have a value attribute? It doesn’t seem to contain the  
> > links text if that’s what you were expecting.
> > 
> > Fred
> 
> Fred - I tried using $(‘test\_link’).innerHtml == ‘one’, but the result  
> is the same.

That should be: innerHTML

Fred
