CSS checkbox styling not working - javascript

I have my checkboxes and I am trying to style it with the following CSS.
input[type="checkbox"]{
display: none;
border: none !important;
box-shadow: none !important;
}
input[type="checkbox"] + label span {
display: inline-block;
vertical-align: middle;
width: 30px;
height: 30px;
background: url(/static/app/images/check_no.svg);
}
input[type="checkbox"]:checked + label span {
background: url(/static/app/images/check_yes.svg);
content: '';
color: #fff;
vertical-align: middle;
width: 30px;
height: 30px;
}
<div class="check1">
<input id="id_contract_name" name="contract_name" type="checkbox">
<label for=" id_contract_name ">
<span class="chk_contract"></span> Name on Contract
</label>
</div>
<div class="check2">
<input id="id_is_ceo" name="is_ceo" type="checkbox">
<label for=" id_is_ceo ">
<span></span> CEO?
</label>
</div>
The checkboxes are inside my form.
This does not work when I click my checkbox. I am new to styling and let me know where the error is.

You have spaces around the name in the for attribute, remove them.
Working demo:
input[type="checkbox"]{
display: none;
border: none !important;
box-shadow: none !important;
}
input[type="checkbox"] + label span {
display: inline-block;
vertical-align: middle;
width: 30px;
height: 30px;
background: url(/static/app/images/check_no.svg);
}
input[type="checkbox"]:checked + label span {
background: url(/static/app/images/check_yes.svg);
content: '';
color: #fff;
vertical-align: middle;
width: 30px;
height: 30px;
/*added this to show the behavior here on SO*/
background-color: #000
}
<div class="check1">
<input id="id_contract_name" name="contract_name" type="checkbox">
<label for="id_contract_name">
<span class="chk_contract"></span> Name on Contract
</label>
</div>
<div class="check2">
<input id="id_is_ceo" name="is_ceo" type="checkbox">
<label for="id_is_ceo">
<span></span> CEO?
</label>
</div>

You have to mention id or class in CSS.
And if you want to add css dynamically then you have to use javascript or jquery.
#id_contract_name{
display: none;
border: none !important;
box-shadow: none !important;
}
#id_is_ceo {
display: inline-block;
vertical-align: middle;
width: 30px;
height: 30px;
background: url(/static/app/images/check_no.svg);
}
input[type="checkbox"]:checked + label span {
background: url(/static/app/images/check_yes.svg);
content: '';
color: #fff;
vertical-align: middle;
width: 30px;
height: 30px;
}
<html>
<body>
<div class="check1">
<input id="id_contract_name" name="contract_name" type="checkbox">
<label for=" id_contract_name ">
<span class="chk_contract"></span> Name on Contract
</label>
</div>
<div class="check2">
<input id="id_is_ceo" name="is_ceo" type="checkbox">
<label for=" id_is_ceo ">
<span></span> CEO?
</label>
</div>
</body>
</html>

Related

CSS align two elements next to each other

I have looked at all the other questions on the same topic but none seem to be working. Consider the code-
.River {
height: 100px;
width: 100%;
margin-top: 30px;
background-color: #00BFFF
}
.switch {
position: relative;
width: 60px;
height: 34px;
float: left;
text-align: center;
}
<div class="River">
<p>River</p>
</div>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
I am trying to position the switch directly in front of the River label. This would look something like
RIVER - 'SWITCH'
Any ideas on how I can achieve this
I think u have useless tags and css... Make it simple deleting all the css for switch checkbox and wrap it all on form control div
.River {
height: 100px;
width: 100%;
margin-top: 30px;
background-color: #00BFFF
}
<div class="River">
<div class="form-control">
<label for="switch-checkbox" class="switch">River</label>
<input id="switch-checkbox" type="checkbox">
</div>
</div>
Here is code
.River {
display: inline-block;
vertical-align: middle;
}
.switch {
display: inline-block;
vertical-align: middle;
margin-left: 10px;
}
.river-wrap {
background-color: #00BFFF;
overflow: hidden;
padding: 15px;
}
<div class="river-wrap">
<div class="River">
<p>River</p>
</div>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</div>
Check That,
.River {
height: 100px;
width: 100%;
margin-top: 30px;
background-color: #00BFFF;
}
.switch {
position: relative;
width: 60px;
height: 34px;
float: left;
text-align: center;
margin-top: 18px;
}
.River p {
display: inline-block;
float: left;
}
<div class="River">
<p>River</p>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</div>
Of course the checkbox won't stay side-by-side with the River because you set width:100% to .River . I changed your html a little bit for that to work
You have multiple solutions for this ( i don't know how you searched and didn't find a solution )
Since you already added float:left yo switch , add float:left to .River p. See snippet
.River {
height: 100px;
width: 100%;
margin-top: 30px;
background-color: #00BFFF
}
.switch {
position: relative;
width: 60px;
height: 34px;
float: left;
text-align: center;
}
.River p {
float: left;
margin:0;
}
<div class="River">
<p>River</p>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</div>
display:inline or inline-block on p and on switch
.River {
height: 100px;
width: 100%;
margin-top: 30px;
background-color: #00BFFF
}
.switch {
position: relative;
width: 60px;
height: 34px;
display:inline;
text-align: center;
}
.River p {
margin:0;
display:inline;
}
<div class="River">
<p>River</p>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</div>
Other ways : using flexbox, grids, tables etc.
Someone already mentioned it in a comment:
Since "River" is a label, it should be inside your label tag. So, if you put it there and wrap .River around the label, you might not get exactly what you want (guessing based on your rules), but you get the checkbox where you want and can work from there.
.River {
height: 100px;
width: 100%;
margin-top: 30px;
background-color: #00BFFF
}
.switch {
position: relative;
width: 60px;
height: 34px;
float: left;
text-align: center;
}
<div class="River">
<label class="switch">River
<input type="checkbox">
<span class="slider round"></span>
</label>
</div>

Fix div position in a form

I am using the same form on two different pages. On the first one every div is tidy in the desired position. On the second page, there is a select that only appears after a change (jquery trigger), which causes the div below it to appear first on the right but after the trigger it moves on the left. I'd like the div which contains radio buttons to be on the left side at all times, just like it is on the first page. The part that I'd like to fix is #center-add-new-event-form .container-field input[type=radio].
This is the css.
#center-add-new-event-form,
#center-add-new-event-form * {
box-sizing: border-box;
}
#center-add-new-event-form {
padding: 20px 10px 0px;
}
#center-add-new-event-form .container-field {
width: 50%;
float: left;
padding: 0 10px;
margin-bottom: 20px;
vertical-align: top;
}
#center-add-new-event-form .container-field label {
display: block;
font-weight: 600;
margin-bottom: 5px;
}
#center-add-new-event-form .container-field input[type=text],
#center-add-new-event-form .container-field select {
height: 34px;
border: 1px solid #ddd;
padding: 0 7px;
display: block;
width: 100%;
}
#center-add-new-event-form .container-field input[type=radio]
{
height: 34px;
border: 1px solid #ddd;
padding: 0 7px;
display: block;
width: 100%;
}
#center-add-new-event-form .center-error-message {
font-size: 0.9em;
color: darkred;
display: block;
margin-left: 1px;
}
.center-event-actions {
border-top: 1px solid #ddd;
padding: 20px 20px 15px;
text-align: right;
}
.clearfix:before,.clearfix:after {
display: table;
content: " ";
}
.clearfix:after {
clear: both;
}
#media screen and (max-width: 767px) {
#center-add-new-event-form .container-field {
float: none;
width: 100%;
}
}
<form id="center-add-new-event-form" class="clearfix">
<div class="container-field" style="width: 100%">
<label></label>
<div id="center-request-details">
</div>
</div>
<div class="container-field">
<label for="teaching-requests"></label>
<select id="input-teaching-requests" name="teaching-requests" class="create-request-event ui-widget-content ui-corner-all">
<option value=""></option>
<option value=""></option>
</select>
<span class="center-error-message"></span>
</div>
<div class="container-field">
<label for="request-date"></label>
<input type="text" name='request-date' class="create-request-event event-datepicker ui-widget-content ui-corner-all">
<span class="center-error-message"></span>
</div>
</div>
<div class="container-field">
<label for="teaching-start-time"></label>
<input type="text" name="teaching-start-time" class="create-request-event event-timepicker text ui-widget-content ui-corner-all">
<span class="center-error-message"></span>
</div>
<span class="clearfix"></span>
<div class="container-field">
<label for="teaching-end-time"></label>
<input type="text" name="teaching-end-time" class="create-request-event event-timepicker text ui-widget-content ui-corner-all">
<span class="center-error-message"></span>
</div>
<div class="container-field">
<label for="teaching-teacher"></label>
<div id='contanier-teacher-selection'>
</div>
<span class="center-error-message"></span>
</div>
<div class="container-field pull-left" align="left">
<label for="recursive-events" align="left">
<input type="radio" align="left" name="recursive" value="daily"/>Daily
<input type="radio" align="left" name="recursive" value="weekly"/>Weekly
<input type="radio" align="left" name="recursive" value="monthly"/>Monthly
</label>
</div>
<span class="center-error-message"></span>
</form>
<div class="center-event-actions">
<button id="cancel-add-new-event" class='button' data-izimodal-close="" data-izimodal-transitionout="bounceOutDown"></button>
<button id="add-new-event" class='button button-primary'></button>
</div>
Before
After
It looks like it is caused by container-field <= width: 50% and float
so when this div appears it appears next to last one
the solution add
<span class="clearfix"></span>
before the the element with radio buttons
This might work:
#center-add-new-event-form .container-field input[type=radio]:before
{
height: 34px;
border: 1px solid #ddd;
padding: 0 7px;
display: block;
width: 100%;
}
#center-add-new-event-form .container-field input[type=radio]:after
{
height: 34px;
border: 1px solid #ddd;
padding: 0 7px;
display: block;
width: 100%;
float: right;
}

How to use jquery to move a div to the top of a element and the other two to the bottom

Hey I'm making this accordion and when you select an option in the top, one of the three other panels have to show. To accomplish this I would like to move the panel in the DOM to the top of the tree. The other two have to be moved to the bottom however. Right now it just switchs to one of the three but if I select a new one it doesn't update so I'm sure the dom is all mixed up.
HTML
<button class="accordion" id="special_accord">
<h2 class="float-left">2.</h2>
<h2 class="text-center">Model</h2></button>
<div class="panel" id="default_panel">
<label>
<h3 class="text-center">Please select a Device Type above</h3></label>
</div>
<div class="panel" id="laptop_panel">
<div id="col1">
<label class="control control--radio">LAPTOP
<input type="radio" name="radio-model" value="laptop1" />
<div class="control__indicator"></div>
</label>
</div>
<div id="col3">
<label class="control control--radio">LAPTOP2
<input type="radio" name="radio-model" value="laptop2" />
<div class="control__indicator"></div>
</label>
</div>
<div id="col2">
<label class="control control--radio">LAPTOP3
<input type="radio" name="radio-model" value="laptop3" />
<div class="control__indicator"></div>
</label>
</div>
</div>
<div class="panel" id="tablet_panel">
<div id="col1">
<label class="control control--radio">iPad 2
<input type="radio" name="radio-model" value="tablet-ipad2" onclick="calculateTotal()" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">iPad 3
<input type="radio" name="radio-model" value="tablet-ipad3" onclick="calculateTotal()" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">iPad 4
<input type="radio" name="radio-model" value="tablet-ipad4" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">iPad Air
<input type="radio" name="radio-model" value="tablet-ipadAir" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">iPad Mini
<input type="radio" name="radio-model" value="tablet-ipadMini" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">iPad Mini 2
<input type="radio" name="radio-model" value="tablet-ipadMini2" />
<div class="control__indicator"></div>
</label>
</div>
<div id="col3">
<label class="control control--radio">Nexus 7
<input type="radio" name="radio-model" value="tablet-nexus7" />
<div class="control__indicator"></div>
</label>
</div>
<div id="col2">
<label class="control control--radio">Amazon Fire
<input type="radio" name="radio-model" value="tablet-amazonFire" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">Amazon Kindle
<input type="radio" name="radio-model" value="tablet-amazonFire" />
<div class="control__indicator"></div>
</label>
</div>
</div>
<div class="panel" id="phone_panel">
<div id="col1">
<label class="control control--radio">iPhone 3 & 4
<input type="radio" name="radio-model" value="phone-iphone3" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">iPhone 5, 5c, 5s
<input type="radio" name="radio-model" value="phone-iphone5cs" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">iPhone 6
<input type="radio" name="radio-model" value="phone-iphone6" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">iPhone 6 Plus
<input type="radio" name="radio-model" value="phone-iphone6+" />
<div class="control__indicator"></div>
</label>
</div>
<div id="col3">
<label class="control control--radio">Microsoft Lumia 430
<input type="radio" name="radio-model" value="phone-lumia430" />
<div class="control__indicator"></div>
</label>
</div>
<div id="col2">
<label class="control control--radio">Galaxy S3
<input type="radio" name="radio-model" value="phone-galaxys3" />
<div class="control__indicator"></div>
</label>
<label class="control control--radio">Galaxy S4
<input type="radio" name="radio-model" value="phone-galaxys4" />
<div class="control__indicator"></div>
</label>
</div>
</div>
CSS
.icon-select {
margin-right: 20px;
margin-left: 20px;
}
#col1 {
float: left;
width: 33%;
height: 100%;
}
#col2 {
float: center;
height: 100%;
width: 33%;
overflow: hidden;
display: table-cell;
}
#col3 {
float: right;
height: 100%;
width: 34%;
margin-left: 20px;
overflow: hidden;
display: table-cell;
}
#price_tally {
float: right;
display: inline-block;
border: 1px solid #6fdd7a;
padding-top: 10px;
border-radius: 2px;
background-color: #6fdd7a;
}
#price_tally hr {
border: 1px solid #ffffff;
margin: 0px;
}
#money_tally {
text-align: center;
font-size: 3em;
padding: 0px;
margin: 0px;
color: #ffffff;
}
#price_tally button {
width: 100%;
height: 100%;
margin: 0px;
padding: 16px;
font-size: 26px;
background-color: #6fdd7a;
color: #ffffff;
border: none;
outline: none;
cursor: pointer;
}
#price_tally button:hover {
background-color: #65c76f;
}
button.accordion {
background-color: #ffffff;
color: #444;
cursor: pointer;
padding: 2px;
width: 50%;
text-align: left;
outline: none;
transition: 0.4s;
border-left: 1px solid #6fdd7a;
border-right: 1px solid #6fdd7a;
border-top: 1px solid #6fdd7a;
border-radius: 4px;
border-bottom: none;
}
button.accordion.active,
button.accordion:hover {
background-color: #6fdd7a;
color: #ffffff;
}
div.panel {
padding: 0px 18px;
background-color: #ffffff;
max-height: 0;
overflow: hidden;
width: 46%;
border-right: 1px dotted #6fdd7a;
border-left: 1px dotted #6fdd7a;
transition: max-height 0.2s ease-out;
}
#optional_panel {
border-bottom: 1px solid #6fdd7a;
}
label > input {
visibility: hidden;
position: absolute;
}
label > input + img {
cursor: pointer;
border: 2px solid transparent;
border-radius: 2px;
-webkit-transition: all 0.25s linear;
}
label > input:checked + img {
background-color: #6fdd7a;
}
.invisible {
display: none;
}
div.showing {
padding: 0px 18px;
background-color: #ffffff;
max-height: 600px;
overflow: hidden;
width: 46%;
max-height: 100%;
border-right: 1px dotted #6fdd7a;
border-left: 1px dotted #6fdd7a;
transition: max-height 0.2s ease-out;
}
.control {
font-size: 18px;
position: relative;
display: block;
margin-bottom: 15px;
padding-left: 30px;
cursor: pointer;
}
.control input {
position: absolute;
z-index: -1;
opacity: 0;
}
.control__indicator {
position: absolute;
top: 2px;
left: 0;
width: 20px;
height: 20px;
background: #e6e6e6;
}
.control--radio .control__indicator {
border-radius: 50%;
}
.control:hover input ~ .control__indicator,
.control input:focus ~ .control__indicator {
background: #444;
}
.control input:checked ~ .control__indicator {
background: #6fdd7a;
}
.control:hover input:not([disabled]):checked ~ .control__indicator,
.control input:checked:focus ~ .control__indicator {
background: #6fdd7a;
}
.control__indicator:after {
position: absolute;
display: none;
content: '';
}
.control input:checked ~ .control__indicator:after {
display: block;
}
.control--checkbox .control__indicator:after {
top: 4px;
left: 8px;
width: 3px;
height: 8px;
transform: rotate(45deg);
border: solid #fff;
border-width: 0 2px 2px 0;
}
.control--checkbox input:disabled ~ .control__indicator:after {
border-color: #7b7b7b;
}
.control--radio .control__indicator:after {
top: 7px;
left: 7px;
width: 6px;
height: 6px;
border-radius: 50%;
background: #fff;
}
.control--radio input:disabled ~ .control__indicator:after {
background: #7b7b7b;
}
JS
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function () {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
}
else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
};
}
var def = $("#special_accord");
var lap = $("#laptop_panel");
var tab = $("#tablet_panel");
var pho = $("#phone_panel");
$("#laptop_button").click(function () {
lap.before().insertAfter(def);
});
$("#tablet_button").click(function () {
tab.before().insertAfter(def);
});
$("#phone_button").click(function () {
pho.before().insertAfter(def);
});
This may not really answer your question. But is this close to your objective?
I am not too sure about your objective here.
$(".accordion").on("click", function(e){
$(".panel").hide();
console.log("selected: ", $(this).attr("data-model"));
var model = $(this).attr("data-model");
if (model) {
$( "#" + model + "_panel").show("slow");
}
else {
$("#default_panel").show("slow");
}
});
// hide all panels
$(".panel").hide();
https://jsfiddle.net/sudarpochong/ncynro4j/

Angular Checkbutton Behaviour wrong

i have a big problem with the (probably) the scopes here but i cannot see through it.
I want to have my own css-modified tooglebuttons behave the same way, as the other buttons already do , i've tried to adopted it from this site:
http://jsfiddle.net/opensas/ye6At/
Here my is my fiddle:
http://jsfiddle.net/fs0tL62s/
My second button is reacting to the values but i cannot control it differently.
Thx in advance.
My code is the following:
my CSS:
.onoffswitch {
position: relative;
width: 92px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block;
overflow: hidden;
cursor: pointer;
border: 2px solid #999999;
border-radius: 20px;
}
.onoffswitch-inner {
display: block;
width: 200%;
margin-left: -100%;
transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before,
.onoffswitch-inner:after {
display: block;
float: left;
width: 50%;
height: 30px;
padding: 0;
line-height: 30px;
font-size: 12px;
color: white;
font-family: Trebuchet, Arial, sans-serif;
font-weight: bold;
box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "Local";
padding-left: 10px;
background-color: #34A7C1;
color: #FFFFFF;
}
.onoffswitch-inner:after {
content: "Remote";
padding-right: 10px;
background-color: #EEEEEE;
color: #999999;
text-align: right;
}
.onoffswitch-switch {
display: block;
width: 22px;
margin: 4px;
background: #FFFFFF;
position: absolute;
top: 0;
bottom: 0;
right: 58px;
border: 2px solid #999999;
border-radius: 20px;
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;
}
my JS:
angular.module('bootstrap', []).directive('button', function() {
return {
restrict: 'E',
require: 'ngModel',
link: function($scope, element, attr, ctrl) {
// ignore other kind of button groups (e.g. buttons-radio)
if (!element.parent('[data-toggle="buttons-checkbox"].btn-group').length) {
return;
}
// set/unset 'active' class when model changes
$scope.$watch(attr.ngModel, function(newValue, oldValue) {
element.toggleClass('active', ctrl.$viewValue);
});
// update model when button is clicked
element.bind('click', function(e) {
$scope.$apply(function(scope) {
ctrl.$setViewValue(!ctrl.$viewValue);
});
// don't let Bootstrap.js catch this event,
// as we are overriding its data-toggle behavior.
e.stopPropagation();
});
}
};
});
angular.module('sample', ['bootstrap']);
function SampleController($scope) {
console.log('model');
$scope.myModel = {
};
}
and my html:
<div ng-app="sample" ng-controller="SampleController">
<ul>
<li>myModel.optionA:{{myModel.optionA}}</li>
<li>myModel.optionB: {{myModel.optionB}}</li>
</ul>
<form class="form-inline">
<label class="checkbox">
<input type="checkbox" ng-model="myModel.optionA"> A
</label>
<label class="checkbox">
<input type="checkbox" ng-model="myModel.optionB"> B
</label>
</form>
<div class="btn-group" data-toggle="buttons-checkbox">
<button type="button" ng-model="myModel.optionA" class="btn">A</button>
<button type="button" ng-model="myModel.optionB" class="btn">B</button>
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox"
id="myonoffswitch" checked ng-model="myModel.optionB">
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox"
id="myonoffswitch" checked ng-model="myModel.optionA">
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</div>
</div>
You have an id collision:
change the id for the last button (and the label "for" attribute to match it) to something different and it works.
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch2" checked ng-model="myModel.optionA">
<label class="onoffswitch-label" for="myonoffswitch2">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
Updated fiddle

How to make a div into a checkbox

I recently started coding and I want to know the best way to make a box into a checkbox. So I want to make a website where the user can choose colors by checking the colored boxes to pick the ones they want. I have researched and can't find a good answer. All the boxes should be clickable, as the user can choose more than one color.
edit I'm sorry if I didn't make sense! What I'm trying to do is something like this: sv.tinypic.com/r/dcelb6/9. So the boxed are colored, and you can check them.
Here is my html:
<div>
<a href="#" class="color-box black-box" value="0">
<h3>Black</h3>
</a>
</div>
<div>
<a href="#" class="color-box grey-box" value="0">
<h3>Grey</h3>
</a>
</div>
<div>
<a href="#" class="color-box blue-box" value="0">
<h3>Blue</h3>
</a>
</div>
</div>
and here is the CSS:
.color-box {
width: 15.20833%;
min-width: 15.20833%;
height: 0;
padding-bottom: 15.20833%;
background-color: #fff;
margin-top: 0.72917%;
display: block;
float: left;
position: relative;
margin: 7px 0.72917%;
border: 1px solid transparent;
}
.color-box h3 {
color: #fff;
text-decoration: none;
}
.black-box {
background: #000;
}
.grey-box {
background: #9E9E9E;
}
.blue-box {
background: #205DB1;
}
where the user can choose colors by checking the colored boxes to pick
the ones they want.
snippet here
colors={'black-box':'black','grey-box':'grey','blue-box':'blue'}
var elements=document.getElementsByClassName('color-box')
function handler(el){
el[i].addEventListener('click',
function(e){
if(e.target.className.split(' ')[1] in colors){
document.getElementById('selector').style.background= colors[e.target.className.split(' ')[1]]
}
for(var i=0;i<elements.length;++i){
if(elements[i]!=e.target){elements[i].innerHTML=''}
}
e.target.innerHTML=='✓'?e.target.innerHTML='':e.target.innerHTML='✓';
},false)
}
for(var i=0;i<elements.length;++i){
handler(elements)
}
//document.getElementsByClassName('color-box').forEach(handler)
.color-box {
color:white;
font-size:20px;
width:30px;
height:30px;
background-color: #fff;
margin-top: 0.72917%;
display: block;
text-align:center;
position: relative;
border: 1px solid transparent;
}
.black-box {
background: #000;
}
.grey-box {
background: #9E9E9E;
}
.blue-box {
background: #205DB1;
}
#selector{
width:200px;
height:200px;
border:solid;
}
<div class="color-box black-box" >
</div>
<div class="color-box grey-box">
</div>
<div class="color-box blue-box">
</div>
</div>
<div id='selector'>
</div>
This is a basic example of how to implement it without JS https://plnkr.co/edit/tvamZjENZSLWnrtthDHP?p=preview
<style>
.checkbox > input[type="checkbox"] { display: none; }
.checkbox > input[type="checkbox"]:checked + label:before {margin-right: 10px; content: "[X]";}
.checkbox > input[type="checkbox"] + label:before {margin-right: 10px; content: "[ ]";}
</style>
<div class="checkbox">
<input type="checkbox" id="test">
<label for="test">HELLO</label>
</div>
Note that i'm using > to indicate the immediate checkbox element descendant of .checkbox element, and the + selector for getting the next LABEL element sibling of that input
This is a pure CSS one, using "hidden" radioset with their label colored
JS Fiddle
.radios{
display:none;
}
.color-box {
width: 50px;
line-height: 50px;
margin-top: 0.72917%;
display: inline-block;
margin: 7px 0.72917%;
color: #fff;
text-align:center;
text-decoration: none;
border: 1px solid transparent;
}
.black-box {
background: #000;
}
.grey-box {
background: #9E9E9E;
}
.blue-box {
background: #205DB1;
}
.radios:checked + label{
text-decoration:underline;
}
#rad1:checked ~ #result{
background-color:black;
}
#rad2:checked ~ #result{
background-color:#9E9E9E;
}
#rad3:checked ~ #result{
background-color:#205DB1;
}
#result{
width:200px;
height:200px;
border:2px orange solid;
}
<div>
<input type="radio" id="rad1" name="rad" class="radios">
<label for="rad1" class="color-box black-box">Black</label>
<input type="radio" id="rad2" name="rad" class="radios">
<label for="rad2" class="color-box grey-box">Grey</label>
<input type="radio" id="rad3" name="rad" class="radios">
<label for="rad3" class="color-box blue-box">Blue</label>
<hr>
<div id="result"></div>
</div>

Categories

Resources