views:

2021

answers:

5
+3  Q: 

Hide a DIV [Rails]

Hiding a DIV would be easy enough in Javascript, but is there some Rails-y way to do it? I can think of some ways to do it by calling Javascript from a partial (.erb), of course, but I'd prefer not to write any Javascript at all. Possible?

Edit: The page is loaded and I would like to hide the DIV after (well, on) an Ajax call, so I'm in one of those render :update blocks.

+1  A: 

Don't really know rails, but can you just output something like style="display:none;" into the div tag?

seanb
I don't know rails either :), but my feeling is that I cannot... I can only touch stuff that is within a DIV or other ID addressable piece of the HTML page.
Yar
bummer :( - guess I'd probably get jquery from the toolbox at that point then.
seanb
Nah, it has prototype and scriptalicious (sp.) by default, but even with plain JS it's easy. But now with the responses here (above) I'm set to go.
Yar
+3  A: 

To render an RJS update from your controller:

respond_to do |format|
  format.html
  format.js { render(:update) { |page| page.hide('element_id') } }
end

You can look up the API for other RJS responses.

Andrew Vit
Awesome, great response. Thank you.
Yar
+4  A: 
render :update do |page|
    page.hide 'div_id'
end

You can throw this in you respond_to block or an RJS template.

Another helpful tip, using the same syntax:

render :update do |page|
    page << 'arbitrary javascript code goes here.'
end
uidzer0
+4  A: 

Or, right in your view:

<%= link_to_function "Toggle", "$('#some_div').toggle();" %>
Matt Darby
So that would not even pass to the server-side, right?
Yar
Yep, pure JS. Hiding a div is definitely JS's domain; passing back and forth to the server to hide a div is overkill.
Matt Darby
Love it. Thanks.
Yar
Finally used this... thanks!
Yar
A: 
<%= link_to_function "Toggle", visual_effect(:toggle_blind, "some_div") %>
Eduardo