Two span elements inside label

Inside label i would like to have two span elements.. see example at jsfiddle: https://jsfiddle.net/c7bz1gf2/

 <div class = 'div_test'>
  <div class="checkbox">
     <input type="checkbox" id="id_chxbox" name="" value="">
     <label for="id_chxbox"><span>Checkbox 1  </span> <span class='detail_view'>  [detail] </span></label>
  </div>

 </div>

(style for checkbox was found in this question: How to change the background color on a input checkbox with css?)

I would like to “alert” only when i click on the “details” span..and when this event fires, it shouldn’t mark checkbox as “checked”..

Can anyone help me with this?

Answer

Try use a <div class='detail_view' style="display: inline;">...</div>, then you might have to fix margin and padding

UPDATE:

$('.detail_view').on('click', function (e) { 
    e.preventDefault(); // this prevents the check
    alert("text");
});
.checkbox input[type="checkbox"] {
    width: auto;
    opacity: 0.00000001;
    position: absolute;
    left: 0;
    margin-left: -20px;
    
}
.checkbox label {
    position: relative;
}
.checkbox label:before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    margin: 4px;
    width: 20px;
    height: 20px;
    transition: transform 0.28s ease;
    border-radius: 3px;
    border: 1px solid ;
}
.checkbox label:after {
  content: '';
    display: block;
    width: 9px;
    height: 4px;
    border-bottom: 2px solid #7bbe72;
    border-left: 2px solid #7bbe72;
    -webkit-transform: rotate(-45deg) scale(0);
    transform: rotate(-45deg) scale(0);
    transition: transform ease 0.25s;
    will-change: transform;
    position: absolute;
    top: 10px;
    left: 9px;
}
.checkbox input[type="checkbox"]:checked ~ label::before {
    color: #7bbe72;
}

.checkbox input[type="checkbox"]:checked ~ label::after {
    -webkit-transform: rotate(-45deg) scale(1);
    transform: rotate(-45deg) scale(1);
}

.checkbox label {
    min-height: 30px;
    display: block;
    padding-left: 30px;
    margin-bottom: 0;
    font-weight: normal;
    cursor: pointer;
    vertical-align: sub;
    font-size: 16px;
}
.checkbox label span {
    position: absolute;
    top: 50%;
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%);
}

.detail_view {
    margin-left: 100px;
}
<!-- Jquery Inclusion -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- actual code -->
<div class = 'div_test'>
  <div class="checkbox">
     <input type="checkbox" id="id_chxbox" name="" value="">
     <label for="id_chxbox">
       <div style="display: inline-block;">Very Long Checkbox Text ................. 1</div>
       <div class='detail_view' style="display: inline-block;">[details]</div>
     </label>
  </div>
 </div>

Leave a Reply

Your email address will not be published. Required fields are marked *