0

i have the following anchor tag thats embedded dynamically to a <ul> tag, when the user clicks on the word TEST i want the checkmark to change colour to blue,i tried the following but its not working,what am i doing wrong?

.checkmark{
display: inline-block;
    width: 22px;
    /* height: 22px; */
    height: 17px;
    -ms-transform: rotate(45deg);
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
    margin-left: 50%;
    margin-bottom: 1px;
}
.checkmark:before {
    content: '';
    position: absolute;
    width: 3px;
    height: 9px;
    background-color: #ccc;
    left: 11px;
    top: 6px;
}

.checkmark {
    cursor: pointer;
}

 .checkmark:after {
        content: '';
        position: absolute;
        width: 3px;
        height: 3px;
        background-color: #ccc;
        left: 8px;
        top: 12px;
    }

input[type="checkbox"]:checked + .checkmark:before,
input[type="checkbox"]:checked + .checkmark:after {
    background-color: blue;
}
<a class="internal" data-destination="local" dataid="66027" data-nodeid="undefined" ><span><input type="checkbox" style="display:none;" id="cb66027 "></span>TEST<label for="cb66027" class="checkmark"></label></a>
john
  • 111
  • 1
  • 9

2 Answers2

2

An <a> wrapped round an <input> will not toggle the checked state when clicked. You should convert the <a> to a <label>.

This means that your current .checkmark label will need to be a different element. I've used a span.

You should also move the <input> outside of it's parent <span> so that the <input> and .checkmark are siblings.

.checkmark {
  display: inline-block;
  width: 22px;
  /* height: 22px; */
  height: 17px;
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
  margin-left: 50%;
  margin-bottom: 1px;
}

.checkmark:before {
  content: '';
  position: absolute;
  width: 3px;
  height: 9px;
  background-color: #ccc;
  left: 11px;
  top: 6px;
}

.checkmark {
  cursor: pointer;
}

.checkmark:after {
  content: '';
  position: absolute;
  width: 3px;
  height: 3px;
  background-color: #ccc;
  left: 8px;
  top: 12px;
}

input[type="checkbox"]:checked+.checkmark:before,
input[type="checkbox"]:checked+.checkmark:after {
  background-color: blue;
}
<label class="internal" data-destination="local" dataid="66027" data-nodeid="undefined">
  <input type="checkbox" style="display:none;" id="cb66027 ">
  TEST
  <span for="cb66027" class="checkmark"></span>
</label>
Turnip
  • 35,836
  • 15
  • 89
  • 111
0

You are using a wrong + selector...

input+.checkmark means... input and .checkmark should be adjacent element...which is not in your case...

...so remove the span and put input and .checkmark in same level...

...also use opacity:0 instead of display:none to input[checkbox]...

I have changes some of your css

Stack Snippet

.checkmark {
  display: inline-block;
  width: 22px;
  /* height: 22px; */
  height: 17px;
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
  margin-left: 50%;
  margin-bottom: 1px;
}

.checkmark:before {
  content: '';
  position: absolute;
  width: 3px;
  height: 9px;
  background-color: #ccc;
  left: 11px;
  top: 6px;
}

.checkmark {
  cursor: pointer;
}

.checkmark:after {
  content: '';
  position: absolute;
  width: 3px;
  height: 3px;
  background-color: #ccc;
  left: 8px;
  top: 12px;
}

input[type="checkbox"]:checked+.checkmark:before,
input[type="checkbox"]:checked+.checkmark:after {
  background-color: blue;
}

input[type="checkbox"] {
  position: absolute;
  width: 100%;
  left: 0;
  z-index: 9;
  height: 100%;
  top: 0;
  cursor: pointer;
  opacity: 0;
  margin: 0;
}

a.internal {
  position: relative;
}
<a class="internal" data-destination="local" dataid="66027" data-nodeid="undefined" for="cb66027"><input type="checkbox" id="cb66027 ">TEST<label class="checkmark"></label></a>
Bhuwan
  • 16,525
  • 5
  • 34
  • 57