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?
 
Tomarse111Connect With a Mentor Commented:
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
 
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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
 
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
 
synergiqAuthor Commented:
Great, thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.