Grey out div onclick

I have a checkbox on my site, when a user checks the checkbox, I want to grey a div out. I have this working with the following javascript (see code snippet).

The code I use for my checkbox is:
<input type="checkbox" name="datecheck" class="topcheck" onclick="disableDiv('userdiv')"/>

Now, I also want to take the grey div out if the user clicks the checkbox again.

How would I do this?
<script>
		function disableDiv(elm) {
		 
			elm = document.getElementById(elm)
		 
		_width = elm.offsetWidth
		_height = elm.offsetHeight
		_top = elm.offsetTop
		_left = elm.offsetLeft
		 
		overlay = document.createElement("div")
		overlay.style.width = _width + "px"
		overlay.style.height = _height + "px"
		overlay.style.position = "absolute"
		overlay.style.background = "#dedede"
		overlay.style.top = _top + "px"
		overlay.style.left = _left + "px"
		 
		overlay.style.filter = "alpha(opacity=50)"
		overlay.style.opacity = "0.5"
		overlay.style.mozOpacity = "0.5"
		 
		document.getElementsByTagName("body")[0].appendChild(overlay)
		}
    </script>

Open in new window

LVL 2
synergiqAsked:
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.

black0psCommented:
Evaluate the status of the checkbox in the Javascript function. If its checked, do one thing. If its unchecked, do another.
<input type="checkbox" name="datecheck" class="topcheck" onclick="disableDiv(this)"/>
<script>
function disableDiv(elm) {
 
if (elm.checked == true) {
_width = elm.offsetWidth
_height = elm.offsetHeight
_top = elm.offsetTop
_left = elm.offsetLeft
                 
overlay = document.createElement("div")
overlay.style.width = _width + "px"
overlay.style.height = _height + "px"
overlay.style.position = "absolute"
overlay.style.background = "#dedede"
overlay.style.top = _top + "px"
overlay.style.left = _left + "px"
                 
overlay.style.filter = "alpha(opacity=50)"
overlay.style.opacity = "0.5"
overlay.style.mozOpacity = "0.5"
                 
document.getElementsByTagName("body")[0].appendChild(overlay)
}
else {
// revert back to original style.
}
}
</script>

Open in new window

0
Tomarse111Commented:
Use the below:
<body>
<form>
	<input type="checkbox" name="datecheck" class="topcheck" id="datecheck" onclick="disableDiv('userdiv')"/>
    
</form>
</body>
<script>
	function disableDiv(elm) {
	 
		 if(document.getElementById('datecheck').checked){
			 elm = document.getElementById(elm)
			 
			_width = elm.offsetWidth
			_height = elm.offsetHeight
			_top = elm.offsetTop
			_left = elm.offsetLeft
			 
			overlay = document.createElement("div")
			overlay.id='overlayDiv';
			overlay.style.width = _width + "px"
			overlay.style.height = _height + "px"
			overlay.style.position = "absolute"
			overlay.style.background = "#dedede"
			overlay.style.top = _top + "px"
			overlay.style.left = _left + "px"
			 
			overlay.style.filter = "alpha(opacity=50)"
			overlay.style.opacity = "0.5"
			overlay.style.mozOpacity = "0.5"
			 
			document.getElementsByTagName("body")[0].appendChild(overlay)
			
		}else{		
			document.getElementsByTagName("body")[0].removeChild(document.getElementById('overlayDiv'))
		
		}
	}
</script>

Open in new window

0
black0psCommented:
But I wouldn't do it like you are doing it either. I would have the div already created on the page in HTML and reference it using an ID. I would create all of the attributes to be greyed, transparent, etc, then I would set its display to none. Based on the status of the checkbox I would set its display to block or none.
0
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.

synergiqAuthor Commented:
Tomarse111: That's the sort of thing I'm looking for! How would I use it if there was more than one box to be greyed out? (so for example if I tick the date checkbox the date div is activated and if I check the users checkbox the users div is activated)

black0ps: I'm not very good with Javascript. Could you give me an example?
0
Tomarse111Commented:
The function I gave above would allow for multiple divs to be coloured out with a small change. All you would do would be pass the div id through the the onclick event. For example:
<body>
<form>
    <input type="checkbox" name="datecheck" class="topcheck" id="datecheck" onclick="disableDiv('userdiv',this)"/>
    <div id="userdiv" style="width:100px; height:100px; background-color:#0000FF;"></div>
	
    <input type="checkbox" name="datecheck" class="topcheck" id="datecheck" onclick="disableDiv('datediv',this)"/>
    <div id="datediv" style="width:100px; height:100px; background-color:#FF0000;"></div>
</form>
</body>
<script>
        function disableDiv(id,chkBox) {
         
                 if(chkBox.checked){
                         elm = document.getElementById(id)
                         
                        _width = elm.offsetWidth
                        _height = elm.offsetHeight
                        _top = elm.offsetTop
                        _left = elm.offsetLeft
                         
                        overlay = document.createElement("div")
                        overlay.id='overlayDiv'+id;
                        overlay.style.width = _width + "px"
                        overlay.style.height = _height + "px"
                        overlay.style.position = "absolute"
                        overlay.style.background = "#dedede"
                        overlay.style.top = _top + "px"
                        overlay.style.left = _left + "px"
                         
                        overlay.style.filter = "alpha(opacity=50)"
                        overlay.style.opacity = "0.5"
                        overlay.style.mozOpacity = "0.5"
                         
                        document.getElementsByTagName("body")[0].appendChild(overlay)
                        
                }else{          
                        document.getElementsByTagName("body")[0].removeChild(document.getElementById('overlayDiv'+id))
                
                }
        }
</script>

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
synergiqAuthor Commented:
Great, thanks.
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.