We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Creating a checkbox instance

I am trying create an instance for checkbox. I tried to use the code from here: 
https://help.syncfusion.com/api/js/ejcheckbox#methods:ischecked

when I try this code it gives me an error.

<div style="display: inline-block" class="panel panel-default">
                   <div style="height: 20px; padding: 0; padding-left: 10px" class="panel-heading"><b>Group By</b></div>
                   <div style="display: inline-block; padding: 6px;" class="panel-body">
                       <table>
                           <tr>
                               <td class="chkrad">
                                   @Html.EJ().CheckBox("check1").Value("1").Text("Column1").ClientSideEvents(c=> c.Change("Change"))
                               </td>
                               <td class="chkrad">
                                   @Html.EJ().CheckBox("check2").Value("2").Text("Column2").ClientSideEvents(c => c.Change("Change"))
                               </td>
                               <td class="chkrad">
                                   @Html.EJ().CheckBox("check3").Value("3").Text("Column3").ClientSideEvents(c => c.Change("Change"))
                               </td>
                           </tr>
                       </table>
                   </div>
                   <script>

                       function Change(args) {
                                        
                           var checkboxesVal = [$("#check1").val(), $("#check2").val(), $("#check3").val()];

                           for (i = 0; i < checkboxesVal.length; i++) {
                               var chkObj = $("#check"+i).data("ejCheckBox");
                               if (chkObj.isChecked()) {
                                   alert(checkboxesVal[i]);
                               }
                           }
                                       
                       }
                   </script>

               </div>

1 Reply

BC Berly Christopher Syncfusion Team June 6, 2017 12:26 PM UTC

Hi Richard, 
Thanks for contacting Syncfusion Support. 
Query1: How to get the checkbox value which are in checked state. 
 
Answer: 
 
We have checked with your query. We suggest you to use simply “args” (arguments from checkbox change event) or “this”(corresponding currently changed control instance) to get the value for the checkbox which are in checked state. Please refer the below code example. 
 
   <script> 
        function Change(args) { 
            if (args.isChecked) 
                alert(args.model.value); 
        } 
    </script> 
 
Query2: How to create the instance for the component. 
Answer: 
 You can create the instance for the component like below. In the below code example, we have created an instance for “check1”. So, you can get the alert for checked status for the first checkbox only. 
 
<script> 
        function Change(args) { 
            // Create CheckBox  instance 
            var chkObj = $("#check1").data("ejCheckBox"); 
          alert(chkObj.isChecked()); // check the status of checkbox 
        } 
    </script> 
 
 
Query 3: 
While using, this code did not get the checked state value. 
   <script> 
 
                       function Change(args) { 
                                         
                           var checkboxesVal = [$("#check1").val(), $("#check2").val(), $("#check3").val()]; 
 
                           for (i = 0; i < checkboxesVal.length; i++) { 
                               var chkObj = $("#check"+i).data("ejCheckBox"); 
                               if (chkObj.isChecked()) { 
                                   alert(checkboxesVal[i]); 
                               } 
                           } 
                                        
                       } 
                   </script> 
Answer: 
We have checked your code example; We could see that you have tried to get the instance of control In loop. And you have stated variable I as 0 and checked. But in your code, you have assigned control’s id starting from 1 (check1). Therefore, we suspect that the issue is due to this. So please refer the modified code example to check the reported issue. 
If you are need to get an alert for every checkbox value which are in checked state bound to the control in your sample, then please modify your given code sample as like below. 
 
<script> 
    function Change(args) { 
        var checkboxesVal = [$("#check1").val(), $("#check2").val(), $("#check3").val()]; 
        for (i = 1; i <= checkboxesVal.length; i++) { 
            var chkObj = $("#check" + i).data("ejCheckBox"); 
            if (chkObj.isChecked()) { 
                alert(checkboxesVal[i - 1]); 
            } 
        } 
 
    } 
</script> 
       
 
For your Convenience, we have prepared sample. Please get the sample from the below location. 
 
To know more about create the instance for the control please refer the below link. 
 
Regards, 
Berly B.C 


Loader.
Up arrow icon