I have a light switch image on my website. It triggers a function (using onClick) which makes the whole wrapper div have an opacity of 0.2 giving the effect of lights being off.
So the switch is clicked and the lights are "off", however I cannot work out how to turn them back on.
function lightSwitch()
{
document.getElementById("switchoff").style.opacity = '0.2';
}
I am very new to JavaScript so I am confident the solution is simple.
I have a light switch image on my website. It triggers a function (using onClick) which makes the whole wrapper div have an opacity of 0.2 giving the effect of lights being off.
So the switch is clicked and the lights are "off", however I cannot work out how to turn them back on.
function lightSwitch()
{
document.getElementById("switchoff").style.opacity = '0.2';
}
I am very new to JavaScript so I am confident the solution is simple.
Share Improve this question edited Apr 25, 2012 at 15:26 Sampson 269k76 gold badges545 silver badges568 bronze badges asked Apr 25, 2012 at 15:25 Sean ZuluSean Zulu 1491 gold badge2 silver badges10 bronze badges 2- 1 Have you tried resetting the opacity back to 1? – Krishna Commented Apr 25, 2012 at 15:28
- "... however I cannot work out how to turn them back on .... " if you mean turn it off after some time, check my answer. – SlavaNov Commented Apr 25, 2012 at 15:38
5 Answers
Reset to default 5function lightSwitch()
{
var el = document.getElementById("switchoff"),
opacity = el.style.opacity;
if(opacity == '0.2')
el.style.opacity = '1';
else
el.style.opacity = '0.2';
}
note - this is not tested, the actual value may differ (".2" or "0.2") - you need to test"
If I understood your question right, you want to turn it off after some time.
So you should use setTimeout
something like this:
function lightSwitch()
{
document.getElementById("switchoff").style.opacity = '0.2';
setTimeout(...function to turn it off...,5000); // 5000 mseconds
}
More info: JS timing
The better approach would be to create a CSS class called 'lightsOut', and add/remove this to your switchoff
element.
function toggleClass( element, className ) {
element.classList.toggle( className );
}
Note, classList
is not implemented in Internet Explorer, so you would need to reference Eli Grey's polyfill for this. In order to use this function, you pass it an element and a class name. So to use it again an element with the id switchOff
, we could do the following:
toggleClass( document.getElementById("switchOff"), 'lightsOut' );
You could give the wrapper div a class in CSS for "on" and another for "off" then toggleClass with JQuery.
$(document).ready(function () {
$(".switchClass").click(function () {
$(this).toggleClass("off");
return false;
});
});
CSS:
div.switchClass{
//Insert your own look and feel
}
div.off.switchClass{
//Same style as div.switchClass except change the opacity
opacity: 0.2;
}
Something like this should work.
function lightSwitch() {
var lightSwitch = document.getElementById("switchoff");
if(lightSwitch.style.opacity != "0.2") //If the div is not 'off'
{
lightSwitch.style.opacity = '0.2'; //switch it 'off'
}
else
{
lightSwitch.style.opacity = '1.0'; // else switch it back 'on'
}
}