Skip to content Skip to sidebar Skip to footer

Css Checkbox Without Input Tag Onclick , Pure Js

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

Solution 1:

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>

Solution 2:

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>

Solution 3:

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

Post a Comment for "Css Checkbox Without Input Tag Onclick , Pure Js"