Friday
Status: New User - Welcome
Joined: 22 Feb 2010
Posts: 3
Back to top
Posted: Feb 22, 10, 23:44    Javascript change class of Element using cookies
What I want to do is have an Layer Div (#introDiv) be visible for first visit, set a cookie so next and subsequent visits that layer will be hidden. CSS has .hide {display:none;}

I have used the method in [link] to set the cookies and that works fine.

I have tried everything I can to try get the layer to hide but haven't been able to figure it out.

In my html I have this:
:: Code ::
<script type="text/javascript">
// name, value, expires, path, domain, secure
if ( Get_Cookie( 'homevisit' ) )changeClass();
Set_Cookie( 'homevisit', 'hide', '7', '/', '', '' );
</script>


In script file:
:: Code ::
function changeClass(){
document.getElementById("introDiv").setAttribute("class","hide");}

This gives an error:
document.getElementById("introDiv")is null
The cookie gets set but the div isn't hidden on refresh.

Any help in getting this to work would be great thanks!!!!
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 2541
Location: East Coast, West Coast? I know it's one of them.
Back to top
Posted: Feb 22, 10, 23:58    
can't say except that getElementById is for ids, not classes. Given you're calling your function something class... it's hard to know if that's what you mean, or if your html is right.

Usually when you get that error it's because the css id isn't located in the running page code.
Friday
Status: New User - Welcome
Joined: 22 Feb 2010
Posts: 3
Back to top
Posted: Feb 23, 10, 4:23    
It works if I put a button with an onclick="change()" on the page then click the button. Seems like it needs an event trigger to active it, it wont active just from if ( Get_Cookie( 'homevisit' ) )changeClass();
Friday
Status: New User - Welcome
Joined: 22 Feb 2010
Posts: 3
Back to top
Posted: Feb 23, 10, 6:19    Found
I found a script that does this. If any one is interested it is at:
scriptden.blogspot.com/2009/07/show-div-once-day-using-javascript.html
Display posts from previous:   

All times are GMT - 8 Hours