user3172663
user3172663

Reputation: 312

How Chk two checkboxes at a time using jQuery

I have two jQuery modal popups with two checkboxes. How to check or uncheck both the check-boxes at the same time?

For example: modal popup1 has chkbox no1 and Modal-popup2 with chkbox no2. I want when the chkbox no1 is checked the chkbox no2 should also be checked and vise-versa.

I have used same class and onclick function for both the chkboxes.

 <input type="checkbox" id="checkboxno1" onClick="clcikchkbox(this)" chartshowvalue="#dialog-chart-setting" class="viz_chart-show_value" value=1/>
 <input type="checkbox" id="checkboxno2" onClick="clcikchkbox(this)" chartshowvalue="#dialog-chart-setting" class="viz_chart-show_value" value=1/>

 function clcikchkbox(elem){
 var addchartShowvalue = $(elem).attr('chartshowvalue');
    if ($(elem).is(':checked')) {
        Dataviz.setting.chart[0].showvalue = 1;
    } else {
        Dataviz.setting.chart[0].showvalue = 0;
    }
}

Upvotes: 1

Views: 208

Answers (2)

Walk
Walk

Reputation: 757

I made a typo in my original comment to your post, this should work (I commented out lines with Dataviz because it's undefined in this snippet.)

 function clcikchkbox(elem){
 var addchartShowvalue = $(elem).attr('chartshowvalue');
    if ($(elem).is(':checked')) {
        //Dataviz.setting.chart[0].showvalue = 1;
    } else {
        //Dataviz.setting.chart[0].showvalue = 0;
    }
    $('.viz_chart-show_value').prop('checked', $(elem).is(':checked'));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="checkboxno1" onClick="clcikchkbox(this)" chartshowvalue="#dialog-chart-setting" class="viz_chart-show_value" value=1/>
<input type="checkbox" id="checkboxno2" onClick="clcikchkbox(this)" chartshowvalue="#dialog-chart-setting" class="viz_chart-show_value" value=1/>

Upvotes: 1

Hamed Taheri
Hamed Taheri

Reputation: 160

if I correctly understand, you want to check both checkboxes at the same time. use something like this :

 function clcikchkbox(){
 var ch1=$('#checkboxno1').is(':checked'); //return true or false
 var ch2=$('#checkboxno2').is(':checked'); //return true or false
 
    if (ch1 && ch2) { //if both checkboxs were checked
        Dataviz.setting.chart[0].showvalue = 1;
    } else {
        Dataviz.setting.chart[0].showvalue = 0;
    }
}

Upvotes: 0

Related Questions