Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2313
  • Last Modified:

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

0
synergiq
Asked:
synergiq
  • 2
  • 2
  • 2
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now