# How to remember if a div is shown or hidden after a page ref

**URL:** <https://www.ruby-forum.com/t/how-to-remember-if-a-div-is-shown-or-hidden-after-a-page-ref/121955>\
**Category:** Rails\
**Created:** [December 3, 2007, 10:59am UTC](https://www.ruby-forum.com/t/how-to-remember-if-a-div-is-shown-or-hidden-after-a-page-ref/121955 "2007-12-03T10:59:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![axplains](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@axplains](https://www.ruby-forum.com/u/axplains)\
**Post date:** [December 3, 2007, 10:59am UTC](https://www.ruby-forum.com/t/how-to-remember-if-a-div-is-shown-or-hidden-after-a-page-ref/121955/1 "2007-12-03T10:59:52Z")

</div>

Hi everyone, hi have a similar problem in a view:

...

Hide/show content

I would like the page to “remember” that the div has been hidden after a  
page reload.  
(The div comes up always visible even if it was set to hidden before).

Any simple way to do this?  
Thanks a lot in advance

---

<div class="post-metadata">

**Author:** ![axplains](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@axplains](https://www.ruby-forum.com/u/axplains)\
**Post date:** [December 3, 2007, 12:30pm UTC](https://www.ruby-forum.com/t/how-to-remember-if-a-div-is-shown-or-hidden-after-a-page-ref/121955/2 "2007-12-03T12:30:07Z")

</div>

Ax Plains wrote:

> Hi everyone, hi have a similar problem in a view:
> 
> ...
> 
> Hide/show content
> 
> I would like the page to “remember” that the div has been hidden after a  
> page reload.  
> (The div comes up always visible even if it was set to hidden before).
> 
> Any simple way to do this?  
> Thanks a lot in advance

If you are reloading the page in the controller, then you can use an  
instance variable in the controller to decide whether the div should be  
hidden or not.Like,

In controller :  
@show\_div\_tag = true

In view :  
\<% default\_style = @show\_div\_tag ? “” : “display:none” %\>

...

You can also optimize this code.

In case if you are manually reloading the browser, the above is  
applicable.

Regards,

- Karthi

---

<div class="post-metadata">

**Author:** ![axplains](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@axplains](https://www.ruby-forum.com/u/axplains)\
**Post date:** [December 3, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/how-to-remember-if-a-div-is-shown-or-hidden-after-a-page-ref/121955/3 "2007-12-03T14:08:06Z")

</div>

Karthi kn wrote:

> Ax Plains wrote:
> 
> > Hi everyone, hi have a similar problem in a view:
> > 
> > ...
> > 
> > Hide/show content
> > 
> > I would like the page to “remember” that the div has been hidden after a  
> > page reload.  
> > (The div comes up always visible even if it was set to hidden before).
> > 
> > Any simple way to do this?  
> > Thanks a lot in advance
> 
> If you are reloading the page in the controller, then you can use an  
> instance variable in the controller to decide whether the div should be  
> hidden or not.Like,
> 
> In controller :  
> @show\_div\_tag = true
> 
> In view :  
> \<% default\_style = @show\_div\_tag ? “” : “display:none” %\>
> 
> ...
> 
> You can also optimize this code.
> 
> In case if you are manually reloading the browser, the above is  
> applicable.
> 
> Regards,
> 
> - Karthi

Thanks a lot.

Anyway, I would like to let the user decide to let the div visible or  
not.  
I think the “show\_div\_tag” variable should be set when the user clicks  
on the link, and remembered between page refreshes (maybe in a session)?

---

<div class="post-metadata">

**Author:** ![axplains](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@axplains](https://www.ruby-forum.com/u/axplains)\
**Post date:** [December 4, 2007, 9:07am UTC](https://www.ruby-forum.com/t/how-to-remember-if-a-div-is-shown-or-hidden-after-a-page-ref/121955/4 "2007-12-04T09:07:53Z")

</div>

Karthi kn wrote:

> Ax Plains wrote:
> 
> > Thanks a lot.
> > 
> > Anyway, I would like to let the user decide to let the div visible or  
> > not.  
> > I think the “show\_div\_tag” variable should be set when the user clicks  
> > on the link, and remembered between page refreshes (maybe in a session)?
> 
> Yes. You can use the session also. if you are not using the session, you  
> have to pass the current value of the variable to the controller for  
> each call(by manually passing the value or by submitting in the form as  
> a hidden value).

Many thanks.  
In the end, I ended up using a cookie…  
works fine and does not confuse Ruby coding.

Thanks a lot anyway.

---

<div class="post-metadata">

**Author:** ![axplains](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@axplains](https://www.ruby-forum.com/u/axplains)\
**Post date:** [December 4, 2007, 7:11am UTC](https://www.ruby-forum.com/t/how-to-remember-if-a-div-is-shown-or-hidden-after-a-page-ref/121955/5 "2007-12-04T07:11:39Z")

</div>

Ax Plains wrote:

> Thanks a lot.
> 
> Anyway, I would like to let the user decide to let the div visible or  
> not.  
> I think the “show\_div\_tag” variable should be set when the user clicks  
> on the link, and remembered between page refreshes (maybe in a session)?

Yes. You can use the session also. if you are not using the session, you  
have to pass the current value of the variable to the controller for  
each call(by manually passing the value or by submitting in the form as  
a hidden value).
