1

I have some RJS in a rails app. I'd like to be able to check the style of an html element before executing an action on that element.
I have tried the following

page << "if ($('current_thread_partial').style.display != 'none'){"

                    puts "HAPPY"

                page << "}else{"

                    puts "SAD"

                page << "}"

But the conditional does not execute. In the example here both HAPPY and SAD are printed to console regardless of the whether display is none or not. Any suggestions or another approach?

Cheers,

Slotishtype

slotishtype
  • 2,715
  • 7
  • 32
  • 47
  • 1
    Are you dead-set on using RJS? Is JQuery an option? – Yule Apr 04 '11 at 12:01
  • Yep. That's why I'm a little confused. I just realized that I am trying to control server side code with a client side conditional...pretty stupid. Cheers – slotishtype Apr 04 '11 at 12:06

1 Answers1

2

To make this code working just do:

page <<JS
if ($('current_thread_partial').style.display != 'none'){
  alert('HAPPY');
}else{
  alert('SAD');
}
JS

This code writes all JavaScript into page variable so it'd be executed by browser.

Your puts between page << "" sentences executes on server side and simple had not been sent to user.

Sector
  • 1,210
  • 12
  • 14