Over a million developers have joined DZone.

Javascript Conditionals In RJS

DZone's Guide to

Javascript Conditionals In RJS

· ·
Free Resource
// From: http://www.thechrisoshow.com/archives/2007/6
// Something you can’t seem to do in RJS is to do conditional checking with the client side DOM.
// For example, I want to be able to investigate an element to see if it has the CSS display tag set to none. The solution is to blend javascript and RJS together.

//To do this you append the javascript as a string to the page object like this:

 # Javascript check to see if the element is visible
page << "if ($('div_element').style.display == 'none') {" 
  # Wow - this is like being in a PROPER if statement!
  # RJS stuff to make the element visible 
  page.replace_html 'div_element', :partial => 'some_partial' 
  page['div_element'].visual_effect :blind_down 
 # back to javascript 
page << '}' 


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}