Skip to content

Instantly share code, notes, and snippets.

Created June 26, 2017 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/270b9260ca5c27b01830800be1eb1dce to your computer and use it in GitHub Desktop.
Save anonymous/270b9260ca5c27b01830800be1eb1dce to your computer and use it in GitHub Desktop.
lightbulb toggle jQ created by dtstanley - https://repl.it/GiQm/285
* {
box-sizing: border-box;
font-family: sans-serif;
}
main {
max-width: 960px;
margin: 0 auto;
min-width: 250px;
padding: 30px;
}
.lightbulb {
border: 1px solid grey;
padding: 10px;
width: 25%;
float: left;
cursor: pointer;
}
.lightbulb img {
width: 100%;
}
.bulb-on {
background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>lightbulb toggle drill</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<link href="index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<main>
<div>
<div class="lightbulb js-lightbulb">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/lightbulb.svg" alt="Kiwi standing on oval">
</div>
<div class="lightbulb js-lightbulb">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/lightbulb.svg" alt="Kiwi standing on oval">
</div>
<div class="lightbulb js-lightbulb">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/lightbulb.svg" alt="Kiwi standing on oval">
</div>
<div class="lightbulb js-lightbulb">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/lightbulb.svg" alt="Kiwi standing on oval">
</div>
</div>
</main>
<script src="index.js"></script>
</body>
</html>
function lightswitch(){
//user clicks element with .js-lightbulb class
$(".lightbulb").click(function(event){
event.stopPropagation();
//other element(s) get .bulb-on removed.
$(".lightbulb").removeClass('bulb-on');
//current element gets the .bulb-on class applied
$(event.currentTarget).addClass("bulb-on");
});
}
$(function(){
lightswitch();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment