conditional javascript

onclick="if(this.style.background='#F9CF00')this.style.background='#ffffff' else this.style.background='#F9CF00';

How do I implement a conditional if else statement in the onclick?

The <tr> is white by default, when a user clicks it, and it is white, I want it to change to orange. And vice versa,

Thanks

onclick ="if(this.style.background='#F9CF00')this.style.background='#ffffff' else this.style.background='#F9CF00';

How do I implement a conditional if else statement in the onclick?

The <tr> is white by default, when a user clicks it, and it is white, I want it to change to orange. And vice versa,

this.style.background = (this.style.background == '#f9cf00' ? '#fffff' : '#f9cf00')

That last part works like this:

(some expression yielding true of false ? return this if true : return this if false)

I think there was a simple typo -

onclick="this.style.background = (this.style.background == '#F9CF00')? '#FF0000' : '#F9CF00';"

You may also want to look into attaching this behind the scenes removing your behavior from your display completely, best practice - but that's just the geek in me.

Jeff.Corcoran wrote:

I think there was a simple typo -

onclick="this.style.background = (this.style.background == '#F9CF00')? '#FF0000' : '#F9CF00';"

Not working D=

You may also want to look into attaching this behind the scenes removing your behavior from your display completely, best practice - but that's just the geek in me.

On Jul 14, 10:17�pm, Justin To <rails-mailing-l...@andreas-s.net>

Yah, once I get it to work I'll stick it somewhere else =)

Try this: onclick="style.backgroundColor=(style.backgroundColor == 'rgb(249, 207, 0)' ? '#FF0000' : '#F9CF00')"

Even when you assign a string like '#ffffff' internally is converted to 'rgb(255, 255, 255)'

Works for me in Firefox 3.0 and Safari 3.1.2

Regards.

If that is still not working - can you drop an example of the code and an error? The above example I gave you worked for me.

Jeff.Corcoran wrote:

If that is still not working - can you drop an example of the code and an error? The above example I gave you worked for me.

Jeff, it works great in Firefox 3.0, but not in IE 7, any suggestions on that?

Thanks!!

Also, I have

onclick = Element.show('<%= user.email -%>') => show <tr>

How do I apply the same logic as the bg color to say, "<tr> is showing, so onlcick hide it, and vice versa":

Element.hide('<%= user.email -%>

Thanks!!

If you can give me an example of the HTML that is coming out I may be able to help you better.

Jeff.Corcoran wrote:

If you can give me an example of the HTML that is coming out I may be able to help you better.

On Jul 15, 10:53�am, Justin To <rails-mailing-l...@andreas-s.net>

How do I get that?

Justin To wrote:

How do I get that?

Just look at the source and then cut and paste

But there is no source for that since it's using ajax w/o refreshing the page. It shows the table, but the source does not reflect the contents

Use firefox. Select the generated html and right click. There is an option for “Show Selection Source”

Or, you can get firebug and either inspect the element or see what the ajax request returned

Element.show('<%= user.email -%>') means that user.email is the id of the tr you want to hide, right? Like this:

  <tr id='<%= user.email %>'> .... </tr>

(which fails btw. if two users on that page have the same email)

Use toggle:   <span onclick='Element.toggle(<%= user.email %>)'>Show/hide it!</

Of course you can't put that into the tr itself because there's no clicking on something invisible.

Oops, forgot the quotes

<span onclick='Element.toggle("<%= user.email %>")'>Show/hide it!</