Cool&Awesome 7 Junior Poster in Training

Hi Everyone,


I have a very simple js function to toggle div visibility, here's the working version:

function Toggle(obj) {	 
	var state = document.getElementById(obj);
	if (state.style.display === 'block') {
		state.style.display = 'none';
	}
	else {
		state.style.display = 'block';
	}
}

Now, as I have multiple divs, I want when new div is opened, the previous to be closed - here's how I tried to solve it, but it doesn't work - help appreciated:)

var prev_obj = 'empty';

 function Toggle(obj) {	 
	var state = document.getElementById(obj);
	if (state.style.display === 'block') {
		state.style.display = 'none';
	}
	else {

                if (prev_obj !== 'empty')
                {
		var prev_state = document.getElementById(prev_obj)
		prev_state.style.display = 'none';
                }		
		state.style.display = 'block';

                prev_obj = obj;
	}
}

Basically, obj is the id of the div to be toggled visible/invisible,
and I thought I can make a prev_obj variable to hold the value of last
selected div, so when new one is opened, i can use it to close the
previous one.