1

I want to make a functional CSS checkbox without using html <input> tag, but struggling on doing it. Can someone check where I went wrong ?

var cbElement = document.getElementsByClassName("checkbox1");

cbElement.addEventListener("click", function() {
  var checkbox = document.getElementsByClassName("check");

  if (checkbox.style.visibility == "hidden") {
    document.getElementByClassName("check").style.visibility = "visible";
  } else if (checkbox.style.visibility == "visible") {
    document.getElementsByClassName("check").style.visibility = "hidden";
  }
});
.checkbox1 {
  border: 1px solid black;
  height: 10px;
  width: 10px;
  display: inline-block;
}

.check {
  visibility: hidden;
  color: black; 
}
<span class="checkbox1">
  <i class="check"></i>
</span>

<span class="checkbox1">
  <i class="check"></i>
</span>

3
  • Why do you want to take input from the user without a form and a control (ie. input)? Commented Dec 2, 2016 at 10:33
  • You can not add event listeners to HTML collections, you must target elements: stackoverflow.com/questions/27834226/… Commented Dec 2, 2016 at 10:44
  • @Esko , There is no real reason , i'm just learning and trying to do different things Commented Dec 2, 2016 at 10:59

3 Answers 3

6

Here is a pure JS solution - some pointers:

  1. getElementsByClassName return an HTMLElement list

  2. Toggling a class name is easier than to edit the styles

  3. The check element must be given a background and not color to get the checked feel.

See demo below:

var cbElements = document.getElementsByClassName("checkbox1");

for (var i = 0; i < cbElements.length; ++i) {
  cbElements[i].addEventListener("click", function() {
    this.getElementsByClassName("check")[0].classList.toggle('active');
  });
}
.checkbox1 {
  border: 1px solid black;
  height: 10px;
  width: 10px;
  display: inline-block;
  cursor:pointer;
}
.check {
  visibility: hidden;
  background: black;
  display: block;
  height: 100%;
}
.active {
  visibility: visible;
}
<span class="checkbox1">
    <i class="check">&nbsp;</i>
</span>

<span class="checkbox1">
    <i class="check">&nbsp;</i>
</span>

Sign up to request clarification or add additional context in comments.

Comments

3

OOP-style with a Checkbox class. You can store references to a Checkbox instances to easily access their "checked" properties in your code. Also "checked" property can be defined with a getter-setter, to make it possible to render a checkbox on property change.

function Checkbox(elem) {
  this.elem = elem;
  this.checked = elem.dataset.checked;
  
  // Extend your component:
  // this.name = ...
  // this.value = ...
  // this.onchange = ...

  elem.addEventListener('click', e => {
    this.checked = !this.checked;
    this.render();
  });
}

Checkbox.prototype.render = function() {
  this.elem.setAttribute('data-checked', this.checked);
}

function initCheckboxes(elems) {
  for (let i = 0; i < elems.length; i++) {
    new Checkbox(elems[i]);
  }
}

initCheckboxes(document.querySelectorAll('.checkbox'));
.checkbox {
  position: relative;
  display: inline-block;
  height: 15px;
  width: 15px;
  border: 1px solid;
  cursor: pointer;
}
.checkbox:after {
  position: absolute;
  width: 100%;
  line-height: 100%;
  text-align: center;
}
.checkbox[data-checked="true"]:after {
  content: "✓";
}
<span class="checkbox"></span>
<span class="checkbox" data-checked="true"></span>

Comments

1
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

<style>
.checkbox{
    border: 1px solid black;
    height: 10px;
    width: 10px;
    display: inline-block;
}
.checked{
    background:red;
}
</style>

<span class="checkbox" id="chk_1">
    <i class="checked"></i>
</span>

<span class="checkbox" id="chk_2">
    <i class="check"></i>
</span>

<script>

var cbElement = $(".checkbox");

$(cbElement).click(function(){
    var currentCb = $(this).attr('id')
    if($("#"+currentCb).hasClass("checked")){
        $("#"+currentCb).removeClass("checked");
    }else {
        $("#"+currentCb).addClass("checked");
    }
});

</script>

5 Comments

OP hasn't used the jQuery tag, why do you assume he wishes to use this?
As Roberrt wrote i want it without jQuery
Because achieving this by Plain JS is like reinventing Wheel again !! Isn't it ??
@TusharKshirsagar , thank you for your time :) but i find jquery usefull for people who already knows how plain JS works. That's why i want to do it first the normal way.
That's absolutely correct approach of learning JS, one must learn first Plain JS then go for Jquery like stuffs. Happy coding !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.