0

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?

zoki182
  • 77
  • 1
  • 6

3 Answers3

1

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>
DDomen
  • 1,808
  • 1
  • 7
  • 17
  • i tried, it almost works,..but the checkbox is still marked as "checked".. :/ – zoki182 Feb 08 '21 at 01:04
  • @zoki182 sorry I forgot about the JS changes – DDomen Feb 08 '21 at 01:40
  • thank you! Could you maybe help me with position of this "new div" element.. i would like to have it positioned always, lets say 10px left of a span element, no matter how text (for example Checkbox1 or Checkbooxxxxxx123) is long.. – zoki182 Feb 08 '21 at 12:01
  • @zoki182 I've edited inserting a snippet, just a note: convert the padding of `.detail_view` to `margin-left: 10px` or you could click the left padded white space and trigger the callback (pad belongs to the element content, margin does not) – DDomen Feb 08 '21 at 13:17
0

Placing the detail_view span outside the label element should do the trick

Oskar Mikael
  • 153
  • 3
  • 17
0

What you need is to stop bubbling the event upwards.

$('.div_test').on('click', 'span.detail_view', function (e) {
   e.stopPropagation()                    
   alert("text");
}
Despina Kastani
  • 832
  • 5
  • 11
  • Hey, thank you for your reply, but this is not working the way i want..it still marks checkbox as checked...but anyway, @DDomen provided me a solution..thank you anyway :) – zoki182 Feb 08 '21 at 10:12