Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 652
bellastic Avatar asked Feb 07 '23 23:02

bellastic


1 Answers

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>
like image 145
repzero Avatar answered Feb 11 '23 01:02

repzero