Make a change in a div when marking a checkbox

3

Does anyone know how I can change a div or label by setting a checkbox example I have this code here.

<label style="margin-bottom: 10px;">
   <div class="adicionalch bg-yellow">
       Nutella <input type="checkbox" name="adicional[]" value="3.00" id="3.00">
  </div>
</label>

With a class bg-yellow , when setting checkbox , it changes to another class or adds some css formatting.

    
asked by anonymous 10.12.2015 / 14:27

2 answers

4

You can do this:

function mudaDiv(el) {
  document.getElementsByClassName("bg-yellow")[0].style.backgroundColor = el.checked ? "blue" : "";
}
<label style="margin-bottom: 10px;">
   <div class="adicionalch bg-yellow">
       Nutella <input type="checkbox" name="adicional[]" value="3.00" id="3.00" onclick="mudaDiv(this);">
  </div>
</label>
    
10.12.2015 / 14:53
3

Is this what you want?

$(function() {
  
  $('.checkbox').change(function() {
     var $that = $(this),
         $div = $that.closest('.bg-yellow');
    
        if ($that.is(':checked')) {
             $div.css('background-color', 'yellow');     
        } else {
            $div.css('background-color', '');
        }
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><labelstyle="margin-bottom: 10px;">
  <div class="adicionalch bg-yellow">
    Nutella <input type="checkbox" name="adicional[]" value="3.00" id="3.00" class="checkbox"></div>
</label>

Instead of picking up id , I had to create a classe because the formatting of id of your checkbox is not jQuery selector compatible.

    
10.12.2015 / 14:32