JSFiddle - React, Tailwind, and code Playground
HTML
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<p>Temos um body com background-image</p><br/><br/>
<div id="button_on-off">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div> <br/><p><= Aqui tenho um botão simples ON/OFF apenas em html/css (sem javascript)</p>
<div id="bloco"></div><br/>
<p>
Isso é só para explicar o que montei, mas o problema é com js.
<br/>
Preciso auterar o background color do bloco div, mas quero ele de uma cor quando o botão estiver "ligado" e de outra cor quando estiver "desligado", e preciso disso com jquery!
tenho o codigo pra trocar a cor mas olha o erro que fica ocorrendo!
</p>
CSS
body {
background-image: url("http://t2.gstatic.com/images?q=tbn:ANd9GcTPOjqC7swgSTY8U_g3ZrF7gyefIfSJ2HZaNEobhlxIOZ6T1Uhb");
}
#button_on-off {
position: relative; width: 63px;
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
margin: 3px 0 0 0px;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block; overflow: hidden; cursor: pointer;
border: 2px solid #999999; border-radius: 50px;
}
.onoffswitch-inner {
display: block; width: 200%; margin-left: -100%;
-moz-transition: margin 0.3s ease-in 0s; -webkit-transition: margin 0.3s ease-in 0s;
-o-transition: margin 0.3s ease-in 0s; transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
display: block; float: left; width: 50%; height: 24px; padding: 0; line-height: 24px;
font-size: 13px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "ON";
padding-left: 7px;
background-color: #18e500;
color: #FFFFFF;
}
.onoffswitch-inner:after {
content: "OFF";
padding-right: 7px;
background-color: #EEEEEE;
color: #999999;
text-align: right;
}
.onoffswitch-switch {
display: block; width: 14px; margin: 5px;
background: #FFFFFF;
border: 2px solid #999999; border-radius: 50px;
position: absolute; top: 0; bottom: 0; right: 35px;
-moz-transition: all 0.3s ease-in 0s; -webkit-transition: all 0.3s ease-in 0s;
-o-transition: all 0.3s ease-in 0s; transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
right: 0px;
}
#bloco {
height: 100px;
width: 200px;
border: 5px solid green;
margin: 20px 0 0 10px;
}
p {
background-color: #fff;
}
JavaScript
$(document).ready(function(){
$('#button_on-off').click(function(){
var cor = $('#myonoffswitch')[0].checked ? 'rgba(0, 0, 0, 0.5)' : 'rgba(0, 0, 255, 0.5)';
$("#bloco").css('background-color', cor);
});
});
// Com o "botão" ligado ele APARECE com a cor correta
// se eu "desligo", também dá certo, mas depois quando clico a cor
// não volta mais a ser transparente assim =
//$(".people").css('background-color', 'rgba(0, 0, 0, 0)');
// Preciso que ( ON ) execulte :
// $("#bloco").css('background-color', 'rgba(0, 0, 0, 0.5)');
// e ( OFF ) execulte:
// $("#bloco").css('background-color', 'rgba(0, 0, 0, 0)');