htmljquery

some code i don't understand well in jquery


1,$('input[id^="checkbox"]').click(function()

what's the id^="checkbox" meaning?

2,

var checkall = $('#checkall');
var boxes = $('input[type="checkbox"]').not(checkall);

checkall.click(function () {
    boxes.attr('checked', this.checked);
});
boxes.change(function() {
    checkall[0].checked = this.checked && boxes.filter(':checked').length === boxes.length;
});

a, var boxes = $('input[type="checkbox"]').not(checkall);

does this line mean "give all type=checkbox input to boxes expect the id=checkall"

what's these lines meaning? checkall[0].checked = this.checked && boxes.filter(':checked').length === boxes.length; and could i change this boxes.attr('checked', this.checked); to boxes.attr('checked', checked); thank you


Solution

    1. all input elements that have id starting with letters "checkbox" like id=checkbox, id=checkbox1, id=checkboxABC etc

    2. var boxes = $('input[type="checkbox"]').not(checkall); means get all input elements of type checkbox except the one with id=checkall

    3. checkall[0].checked = this.checked && boxes.filter(':checked').length === boxes.length; set $('#checkall')'s checked property to true if all checkboxes in the boxes is checked. This is done by checking if the total number of checked checkboxes is equal to total number of checkboxes. The first part this.checked is not required but it doesn't hurt. If current element if unchecked this first part will prevent the second part from executing and will set the status to false which is what you want.

    4. No, you can't change boxes.attr('checked', this.checked) to boxes.attr('checked', checked). You could've tried that yourself.

    if anyone is wondering how I answered these, I was helping OP solve this question yesterday and I know what OP was asking.