multiple checkbox validation is not working

multiple checkbox validation is not working

by deva m -
Number of replies: 4

Hi,

I had implemented  checkbox in enrollment page for users.....this checkbox will appear for each users in the last column.

error is if check it by alert() in javascript.....only for first user it indicating the result whether checked or not

for users alert same message as of for first user...

example:

first user: checked (alert is checked in)and then i unchecked(alert is checked out) it

second user: checked(alert is checked out) and then i unchecked(alert is checked out) it

third user: checked(alert is checked out) and then i unchecked(alert is checked out) it..... and so on

how to solve this..

Attachment checkbox.JPG
Average of ratings: -
In reply to deva m

Re: multiple checkbox validation is not working

by Iban Cardona i Subiela -

Hello Deva,

Could you show us your javascript code so that we can try to help you.

Best regards,
Iban Cardona.

In reply to Iban Cardona i Subiela

Re: multiple checkbox validation is not working

by deva m -
Thank u for the reply....hereby

location:www/totaralms/enrol/renderer.php

$output .= html_writer::empty_tag('input', array('type' => 'checkbox', 'class' => 'checkclass', 'id' => 'checkname','check' => 'download','onclick' => 'MyFunction('."$userid,$courseid".')'));

The above has been given in the renderer file for displaying the checkbox

location:www/totaralms/enrol/users.php

in this i had checkedin by 2 codes
first,
MyFunction(){
var i=0;
 var checkoutHistory = document.getElementById('checkname');
    if (checkoutHistory.checked) {
       
        alert("You have elected to checkin.");
       
    } else {
        alert("You have elected to checkout.");
            }
}


the above code has issue of as i  already mentioned before...

secondly,
MyFunction(){
var i, primi = document.getElementById('checkname');
for (i = 0; i <= primi.length; i++){
    if (primi[i].checked){
        alert('yes');
        return true;
    } else{
        alert('error');
        return false;
    }
}}


it doesnt have any effect and all.....

In reply to deva m

Re: multiple checkbox validation is not working

by Iban Cardona i Subiela -

Hello,

I suggest these changes:

MyFunction()  {

Array.from(document.getElementsByClassName("checkclass")).forEach(function(element) {

   if(element.checked) {

       alert("Element with id "+element.id+" checked");

   } else {

      alert("Element with id "+element.id+" no checked");

   }

});

Best regards,
Iban Cardona.

Average of ratings: Useful (1)
In reply to Iban Cardona i Subiela

Re: multiple checkbox validation is not working

by deva m -

thanks a lot.....for that piece of code..


but each time when checkbox is checkedin/checkedout...

alert message is shown twice...

for 1st checkbox

checkedin status:

->checked in

->no checked

alerts shown

checkedout status:

->no checked

->no checked

alerts shown

for 2nd checkbox

checkedin status:

->no checked

->checked in

alerts shown

checkedout status:

->no checked

->no checked

alerts shown

and so on.....




one more thing can I use checkbox id or name instead of classname as you given.....