0

Here is my code for select unselect checkbox using Jquery:

<input id="demo_box_0" class="checkAll css-checkbox chk_box" type="checkbox" onclick="selectAll(this)" title="Select All"/>

other checkbox are :

<input id=\"demo_box_".$row->CompanyID."\" class=\"css-checkbox csscheck\" type=\"checkbox\" value=\"".trim($row->CompanyID)."\"  />

As i have lengthy class name i am not getting expecting result

Here is my script :

$('.checkAll css-checkbox chk_box').click(function() {
    if ($(this).is(':checked')) {
        $('div input').attr('checked', true);
    } else {
        $('div input').attr('checked', false);
    }
});

What is the mistake i am doing and how can i fix this ?

Update :

I even tried this

<script>
$(document).ready(function(){
$("#demo_box_0").click(function(){
        //alert("just for check");
        if(this.checked){
            $('.checkAll css-checkbox chk_box').each(function(){
                this.checked = true;
            })
        }else{
            $('.checkAll css-checkbox chk_box').each(function(){
                this.checked = false;
            })
        }
    });
});

</script>

1 Answer 1

1

The selector is wrong, it should be:

$('.checkAll.css-checkbox.chk_box');

The above selector selects all the elements that have those 3 classNames. if you want it to just select the checkboxes that have those classNames you can also add the element type to the selector:

$('input[type=checkbox].checkAll.css-checkbox.chk_box');

Also for modifying properties you should use the prop method:

$('.checkAll.css-checkbox.chk_box').on('change', function() {
    $('div input').prop('checked', this.checked);
});
Sign up to request clarification or add additional context in comments.

7 Comments

But this is the class name for other checkbox , css-checkbox csscheck, wil that work ?
Should i remove the onclick function on checkbox ?
The above selector selects the elements that have those 3 classNames, it works just like css selectors. There is no need to specify all the classNames of the elements for selecting them; however, if there is a need for this, you can use it. You are using jQuery, preferably you shouldn't use event-related attributes.
But in console i got Uncaught ReferenceError: selectAll is not defined : onclick
@BizDev You can simply remove the onclick attribute, it has no effect on the above snippet.
|

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.