3

What I'm looking for is when I check "all" all other elements are checked and when I uncheck it all others are unchecked.

$(".dropdown dt a").on('click', function() {
  $(".dropdown dd ul").slideToggle('fast');
});

$(".dropdown dd ul li a").on('click', function() {
  $(".dropdown dd ul").hide();
});

function getSelectedValue(id) {
  return $("#" + id).find("dt a span.value").html();
}

$(document).bind('click', function(e) {
  var $clicked = $(e.target);
  if (!$clicked.parents().hasClass("dropdown")) $(".dropdown dd ul").hide();
});

$('.mutliSelect input[type="checkbox"]').on('click', function() {

  var title = $(this).closest('.mutliSelect').find('input[type="checkbox"]').val(),
    title = $(this).val() + " ";

  if ($(this).is(':checked')) {
    var html = '<span title="' + title + '">' + title + '</span>';
    $('.multiSel').append(html);
    $(".hida").hide();
  } else {
    $('span[title="' + title + '"]').remove();
    var ret = $(".hida");
    $('.dropdown dt a').append(ret);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<dl class="dropdown">
  <dt>
    <a>
      <span class="hida">Choisir :</span>
      <p class="multiSel"></p>
    </a>
    </dt>
  <dd>
    <div class="mutliSelect">
      <ul>
        <li> <input type="checkbox" name="ALL" value="All"> All</li>
        <li> <input type="checkbox" name="check_list[]" value="val1">val1</li>
        <li> <input type="checkbox" name="check_list[]" value="val2">val2</li>
        <li> <input type="checkbox" name="check_list[]" value="val3">val3</li>
      </ul>
    </div>
  </dd>
</dl>

https://jsfiddle.net/8kwb54md/1/

11
  • Show the HTML that this is meant to operate on. Commented Sep 30, 2018 at 17:26
  • @connexo check the full code buddy Commented Sep 30, 2018 at 17:27
  • 2
    "Why doesn't my code work?" requires a minimal reproducible example which shows the actual problem in the question itself and not only a link to an external resource. Commented Sep 30, 2018 at 17:29
  • 1
    That's why we require a minimal example. Strip anything that isn't related to the problem. And as you can see on my edit it is absolutely possible to supply such a minimal example... Commented Sep 30, 2018 at 17:33
  • 1
    Possible duplicate of Select all checkboxes with jQuery Commented Sep 30, 2018 at 17:49

4 Answers 4

2

I reduced your HTML to the list with the checkboxes:

document.querySelector('[name=ALL]').addEventListener('change', (e) => {
  let checkboxes = e.target.closest('ul').querySelectorAll('input[type=checkbox]');
  Array.from(checkboxes).forEach((cb) => {
    cb.checked = e.target.checked;
  });
})
<ul>
  <li> <input type="checkbox" name="ALL" value="All">All</li>
  <li> <input type="checkbox" name="check_list[]" value="val1">val1</li>
  <li> <input type="checkbox" name="check_list[]" value="val2">val2</li>
  <li> <input type="checkbox" name="check_list[]" value="val3">val3</li>
</ul>

If you insist on using jQuery, here you go:

$('[name="ALL"]').on('change', function() {
  var self = this;
  $(this).closest('ul').find('input[type="checkbox"]').each(function() {
    this.checked = self.checked;
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li> <input type="checkbox" name="ALL" value="All">All</li>
  <li> <input type="checkbox" name="check_list[]" value="val1">val1</li>
  <li> <input type="checkbox" name="check_list[]" value="val2">val2</li>
  <li> <input type="checkbox" name="check_list[]" value="val3">val3</li>
</ul>

Sign up to request clarification or add additional context in comments.

4 Comments

@issam The StackOverflow way of saying "thanks" is upvoting.
oh ok but i can't cuz i'm below 15 lvl :)
@connexo The Stack Overflow way is to mark questions as duplicates, not answer them... :P
@HereticMonkey I like to add non-jquery answers to jquery questions, though :)
1

You should look at each()

Here is a bit of help :

$('[name="ALL"]').on('click', function(){
    $('.mutliSelect input[type="checkbox"]').each(function(){
        $(this).prop('checked') ?  $(this).prop('checked','') : $(this).prop('checked','checked')
    })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul class="mutliSelect">
  <li> <input type="checkbox" name="ALL" value="All">All</li>
  <li> <input type="checkbox" name="check_list[]" value="val1">val1</li>
  <li> <input type="checkbox" name="check_list[]" value="val2">val2</li>
  <li> <input type="checkbox" name="check_list[]" value="val3">val3</li>
</ul>

6 Comments

And if he unchecks ALL?
The property checked is either true or false.
@Baldráni thanks dude it works perfectly but how about whene i uncheck "all" the rest are unchecked too
@connexo I'm sorry but I don't get your point with your remark
$(this).prop('checked','') is setting HTMLInputElement.prototype.checked which is a boolean property on the element to an empty string.
|
0
$(".mutliSelect input:checkbox").first().click(function(){
    $('.mutliSelect input:checkbox').not(this).prop('checked', this.checked);
});

This code will select the first input checkbox, hook it to the click event, then it will check all the remaining checkboxs but not the first one, if the first checkbox is checked it will check them all otherwise uncheck them all.

Bon courage.

1 Comment

mrc c tres gentil ;)
0

We need to uncheck all checkbox if any of the options are unchecked.

var checkBoxAll = $('ul li> input[name="ALL"]');
var checkBoxOptions = $('ul li> input:not([name="ALL"])');
checkBoxAll.on('change', function() {
  var self = this;
  $(this).closest('ul').find('input[type="checkbox"]').each(function() {
    this.checked = self.checked;
  })
})

// to check or uncheck All checkbox, if all the options are checked.
checkBoxOptions.on('change', function() {
    var isAllChecked = true;
  
  // to find if any of the options is false.
  $(this).closest('ul').find('li> input:not([name="ALL"])').each(function() {
    if (!this.checked) return isAllChecked = false;
  })
  
  checkBoxAll.get(0).checked = isAllChecked;
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li> <input type="checkbox" name="ALL" value="All">All</li>
  <li> <input type="checkbox" name="check_list[]" value="val1">val1</li>
  <li> <input type="checkbox" name="check_list[]" value="val2">val2</li>
  <li> <input type="checkbox" name="check_list[]" value="val3">val3</li>
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.