Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can't trigger the customized checkbox when the box is ticked

When i click the checkbox nothing happens.

I’m trying to remove the input checkbox and replace it with a customized one .

But when i trigger span , when the box is ticked , nothing happens.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Could anyone tell me why why is this command not executing ?

[type=checkbox]:checked + span:after […]

.preferences{
        height: 103px;
        width: 491px;
        box-shadow: 0px 12px 18px -6px rgba(5, 5, 5, 0.3);
        font-family: 'Roboto',Helvetica,Arial,Lucida,sans-serif;
        font-size: 16px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
}

        input{
            display: none; /* Hide the default checkbox */
          }
          
          /* Style the artificial checkbox */
          span{
            height: 15px;
            width: 15px;
            border: 1px solid grey;
            display: inline-block;
            position: relative;
          }
          
          /* Style its checked state...with a ticked icon */
          [type=checkbox]:checked + span:after {
            content: '\2714';
            position: absolute;
            top: -5px;
            left: 0;
          }
    }
<div class="preferences">
                  <h2>I prefer</h2>


                  
               <label for="prefer-email">
                    <input type="checkbox">
                          <span></span>
                   Send me an email
               </label>
    
               <label for="prefer-call">
                   <input type='checkbox'>
                       <span></span>
                  Call me
               </label>

            </div>

>Solution :

Your labels have a for attribute but your inputs do not have a matching id. This is required to get them to work together. See the link below for a more in depth explanation.

https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/for

Here is a working version of your code:

.preferences {
  height: 103px;
  width: 491px;
  box-shadow: 0px 12px 18px -6px rgba(5, 5, 5, 0.3);
  font-family: 'Roboto',Helvetica,Arial,Lucida,sans-serif;
  font-size: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
input {
  display: none;
}
span {
  height: 15px;
  width: 15px;
  border: 1px solid grey;
  display: inline-block;
  position: relative;
}
[type=checkbox]:checked + span:after {
  content: '\2714';
  position: absolute;
  top: -5px;
  left: 0;
}
<div class="preferences">
<h2>I prefer</h2>
<label for="prefer-email">
<input id="prefer-email" type="checkbox">
<span></span>Send me an email
</label>

<label for="prefer-call">
<input id="prefer-call" type='checkbox'>
<span></span>Call me
</label>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading