Dan Rosenstark
Dan Rosenstark

Reputation: 69747

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.

Upvotes: 7

Views: 8979

Answers (5)

Matt Darby
Matt Darby

Reputation: 6324

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

Upvotes: 11

Eduardo
Eduardo

Reputation: 11

<%= link_to_function "Toggle", visual_effect(:toggle_blind, "some_div") %>

Upvotes: 1

ben lemasurier
ben lemasurier

Reputation: 2592

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

Upvotes: 6

Andrew Vit
Andrew Vit

Reputation: 19239

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.

Upvotes: 3

seanb
seanb

Reputation: 6954

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

Upvotes: 1

Related Questions