How to make div in checkbox

I recently started coding and I want to know how best to make a field in a field. Therefore, I want to create a website where the user can select colors by checking the colored squares to select the ones they want. I researched and cannot find a good answer. All boxes must be interactive, as the user can select more than one color.

edit 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 box is painted, and you can check them out.

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;
}
+4
source share
3 answers

, , , .

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>
Hide result
+4

, 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>

, >, - checkbox .checkbox, + LABEL

+2

This is pure CSS using a β€œhidden” radioset with color labelcolor

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>
Run codeHide result
+2
source

Source: https://habr.com/ru/post/1625231/


All Articles