Javascript change class of Element using cookies
Friday
Status: New User - Welcome
Joined: 22 Feb 2010
Posts: 3
Reply Quote
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!!!!
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
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.
Back to top
Friday
Status: New User - Welcome
Joined: 22 Feb 2010
Posts: 3
Reply Quote
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();
Back to top
Found
Friday
Status: New User - Welcome
Joined: 22 Feb 2010
Posts: 3
Reply Quote
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
Back to top
Display posts from previous:   

All times are GMT - 8 Hours