Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 853
Dan Rosenstark Avatar asked Feb 12 '09 22:02

Dan Rosenstark


3 Answers

Or, right in your view:

For a class-specified div:

<%= link_to_function "Toggle", "$('.some_div').toggle()" %>

For an ID-specified div:

<%= link_to_function "Toggle", "$('#some_div').toggle()" %>

(notice the hash-mark)

Added period to class specific div and hash to id specific div

like image 165
Matt Darby Avatar answered Sep 24 '22 07:09

Matt Darby


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
like image 43
ben lemasurier Avatar answered Sep 20 '22 07:09

ben lemasurier


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.

like image 32
Andrew Vit Avatar answered Sep 21 '22 07:09

Andrew Vit