Show hide cookie amend

Hi,

I've got a cookie i'm using with a show/hide div, I need to configure it to work with more divs.

I've attached the full code, the bit I need to edit is...

function init()
{
  var prev = readCookie('featureIntro');
  if( prev !== null )
    document.getElementById('featureIntro').style.display = prev;
  else
    document.getElementById('featureIntro').style.display='';
}

I tried the following to also use the cookie with the filter div im show/hiding.

function init()
{
  var prev = readCookie('featureIntro');
  if( prev !== null )
    document.getElementById('featureIntro').style.display = prev || document.getElementById(filter).style.display = prev;
  else
    document.getElementById('featureIntro').style.display='' || document.getElementById(filter).style.display='';
}


Any ideas how I can do this?
window.onload=init;
/*function init()
{
	var prev = readCookie('featureIntro');
	if( prev !== null )
		document.getElementById('featureIntro').style.display = prev;
}*/
function init()
{
  var prev = readCookie('featureIntro');
  if( prev !== null )
    document.getElementById('featureIntro').style.display = prev;
  else
    document.getElementById('featureIntro').style.display='';
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
function showhide(id){
	if (document.getElementById){
		obj = document.getElementById(id);
		if (obj.style.display == "none"){
			obj.style.display = "";
		} else {
      		obj.style.display = "none";
		}
		if( null!==readCookie(id) )
		{
			eraseCookie( id );
		}
		createCookie(id,obj.style.display,365);
	}
}

Open in new window

danwpetersAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bane83Commented:

function init()
{
  var prev = readCookie('featureIntro');
  if( prev !== null )
  {
    document.getElementById('featureIntro').style.display = prev; 
    document.getElementById('filter').style.display = prev;
  }
  else
  {
    document.getElementById('featureIntro').style.display='';
    document.getElementById('filter').style.display='';
  }
}

Open in new window

0
danwpetersAuthor Commented:
Hi, thanks but this has bugged it up. it remembers if I close them both, if I open filter it wont remember, if I open feature it will open both when i reload the page.
0
Michel PlungjanIT ExpertCommented:
It would be smarter to save the name of the shown div or a list of shown divs and hide the rest.

Anyway

var listOfItems = ["featureIntro","filter"];
 
function init() {
  for (var i=0;i<listOfItems.length;i++) {
    var prev = readCookie(listOfItems[i]);
    document.getElementById(listOfItems[i]).style.display = (prev != null)?prev:'none'; 
  }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

danwpetersAuthor Commented:
Thanks mplungjan, they open and close ok again now, but reset when the page is reloaded.
0
danwpetersAuthor Commented:
Tested it in firefox, and it works. But in IE it doesnt, forgets your div show/hide state when reloading the page.
0
Michel PlungjanIT ExpertCommented:
remove the window.onload=init and put this at the bottom of the page

<script>
init()
</script>
0
danwpetersAuthor Commented:
Hmm still the same, any ideas?
0
Bane83Commented:
Not sure if this is your problem, or if you've fixed it yet or not, but I believe there might be a small mistake with mplungjian's script:

var listOfItems = ["featureIntro","filter"];
 
function init() {
  for (var i=0;i<listOfItems.length;i++) {
    var prev = readCookie(listOfItems[i]);
    document.getElementById(listOfItems[i]).style.display = (prev != null)?prev:'none';
  }
}

Here, if the cookie is null it sets the display to 'none', which would start the divs as hidden.  Going by your original code, you may want to switch that to:

document.getElementById(listOfItems[i]).style.display = (prev != null)?prev:'';
0
danwpetersAuthor Commented:
bane, thanks for fixing it, I want to give you points too, how can I?
0
Bane83Commented:
There's a split functionality somewhere, I don't remember exactly where, but it's fine.  mplungjan did all of the real work.
0
Michel PlungjanIT ExpertCommented:
And our joint efforts was only worth a "B" ?
0
danwpetersAuthor Commented:
Hi mplungjan, I gave you a B as it wasnt complete, I was then going to give bane83 the same as he fixed the issue, but I couldnt assign any more points after accepting your answer. Sorry.
0
Michel PlungjanIT ExpertCommented:
I unaccepted for you to split points

I do not believe a "B" is in order when the fix was so obvious and I could have answered it myself had I not been asleep at the time (CET) -  so I did not get a chance to rectify the suggestion.
The actual joint solution is worth an "A" in my opinion.
0
danwpetersAuthor Commented:
Thanks for your help guys
0
danwpetersAuthor Commented:
i've marked it correctly now, thanks for unaccepting the points mplungian, it was the first time ive had to accept 2 answers in a question so wasnt sure how it worked correctly. I thought I would have to give a B to yours and an A to bane's as he completed it, if you had completed it I would have given you an A, however, at the end of the day it was bane who completed it, which is why I originally marked you as a B.

Your both A now, thanks again for your help!
0
Michel PlungjanIT ExpertCommented:
Thanks

My issue was really that I had presented you with some code and in 4 hours you closed it with a "B"
Had I been available when you discovered that my code assumed (not unreasonably) that you wanted to default the divs to collapsed, then I would have completed the question and you would have had no reason to give a "B". So basically I suggest you give the experts the time/chance to answer the question completely (with possible extra elaboration) instead of grading based on the first comment.
0
danwpetersAuthor Commented:
mplungian, I graded when my question was fully answered. The code wasn't working correctly in IE, you gave a response to this which did not work, Bane83 then stepped in and provided a fix, and so my question was answered fully. Which would suggest your answer was a B, as it had a bug, and Bane corrected it which made it an A. Your grades are fine now, so lets call this a day.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.