1

I want to have one checkbox that will check all others and I'm stuck. I'm trying to do that with a loop but that returns nothing.

It's an exercise so I cant change the html ids.

HTML:

       <p>
        Price for adds: <span id="price">0 usd</span>
    </p>
</div>

<div class='panel'>
    <div class='panel-body'>
        <form>
            <div class="checkbox">
                <label><input type="checkbox">All adds</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox" data-price="3.50">Additional cheese, 3,50 USD</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox" data-price="2.20">Salami, 2,20 USD</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox" data-price="5.00">Additional Ham, 5 USD</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox" data-price="4.10">pepper, 4,10 USD</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox" data-price="3.50">Mushroms, 3,50 USD</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox">Clear all</label>
            </div>
            <br>
            <p>
                <button class='btn btn-primary'>Submit</button>
            </p>
        </form>
    </div>
</div>

JS:

$(function(){

        $('.panel-body').on('change','input',function(e){


           if($(this).parent().text()=='All adds'){
                var inps = $('.checkbox input');

                    $(inps).each(function{
                        $(this).prop('checked', true)
                    })


                var price = $('#price').html()
                price = price.replace("USD", "");
                price = parseFloat(price)
                var add = $(this).attr('data-price')

                if ($(this).prop('checked')) {
                    console.log($(this).text())
                    var totalPrice = parseFloat(add) + parseFloat(price)

                    $('#price').html(totalPrice.toFixed(2))
                }
                else {
                    var totalPrice =  parseFloat(price) - parseFloat(add)
                    $('#price').html(totalPrice.toFixed(2))
                }
        })


})
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Marduuk
  • 57
  • 1
  • 7

3 Answers3

0

You have a typo:

$(inps).each(function{
                        $(this).prop('checked', true)
                    })  

You should change this:
$(inps).each(function{

To this:
$(inps).each(function(){

Sagiv b.g
  • 30,379
  • 9
  • 68
  • 99
0

1st you are missing () after function:

  $(inps).each(function() {
    $(this).prop('checked', true)
  })

2nd use trim() to remove whitespace from both sides$(this).parent().text().trim()

3rd the if you have is closed with )} which is wrong, just use )

4th Instead of using each you can just do $('.checkbox input').prop('checked', true):

  if ($(this).prop('checked')) {
    $('.checkbox input').prop('checked', true)
  } else {
    $('.checkbox input').prop('checked', false)
  }

This allowed you to uncheck all as well.

$(function() {

  $('.panel-body').on('change', 'input', function(e) {

    if ($(this).parent().text().trim() == 'All adds') {

      if ($(this).prop('checked')) {
        $('.checkbox input').prop('checked', true)
      } else {
        $('.checkbox input').prop('checked', false)
      }

      var price = $('#price').html()
      price = price.replace("USD", "");
      price = parseFloat(price)
      var add = $(this).attr('data-price')

      if ($(this).prop('checked')) {
        console.log($(this).text())
        var totalPrice = parseFloat(add) + parseFloat(price)

        $('#price').html(totalPrice.toFixed(2))
      } else {
        var totalPrice = parseFloat(price) - parseFloat(add)
        $('#price').html(totalPrice.toFixed(2))
      }
    }

  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <p>
    Price for adds: <span id="price">0 usd</span>
  </p>
</div>

<div class='panel'>
  <div class='panel-body'>
    <form>
      <div class="checkbox">
        <label>
          <input type="checkbox">All adds</label>
      </div>
      <div class="checkbox">
        <label>
          <input type="checkbox" data-price="3.50">Additional cheese, 3,50 USD</label>
      </div>
      <div class="checkbox">
        <label>
          <input type="checkbox" data-price="2.20">Salami, 2,20 USD</label>
      </div>
      <div class="checkbox">
        <label>
          <input type="checkbox" data-price="5.00">Additional Ham, 5 USD</label>
      </div>
      <div class="checkbox">
        <label>
          <input type="checkbox" data-price="4.10">pepper, 4,10 USD</label>
      </div>
      <div class="checkbox">
        <label>
          <input type="checkbox" data-price="3.50">Mushroms, 3,50 USD</label>
      </div>
      <div class="checkbox">
        <label>
          <input type="checkbox">Clear all</label>
      </div>
      <br>
      <p>
        <button class='btn btn-primary'>Submit</button>
      </p>
    </form>
  </div>
</div>
Dalin Huang
  • 11,212
  • 5
  • 32
  • 49
-1

Give your main checkbox an id "cAll". and use jQuery:

    $("#cAll").click(function(){
        $('input:checkbox').not(this).prop('checked', this.checked);
    });
effone
  • 2,053
  • 1
  • 11
  • 16