Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

identify atleast one checkbox is checked in array of checkbox with multiple div

iam working with a project online examination quiz

my code

  @foreach($assessments->assessmentsubdetails as $question)
<div id="questions{{ $loop->iteration }}" name="questions[]">

question description

 <input type="checkbox" name="stud_answer{{ $loop->iteration }}[]" id="A" value="A">
<input type="checkbox" name="stud_answer{{ $loop->iteration }}[]" id="B" value="B">
<input type="checkbox" name="stud_answer{{ $loop->iteration }}[]" id="C" value="C">
<input type="checkbox" name="stud_answer{{ $loop->iteration }}[]" id="D" value="D">

</div>
@endforeach

i have question list in side

 <ul class="all-option">

    @foreach($assessments->assessmentsubdetails as $question)

    <li><a href="#questions{{ $loop->iteration }}" class="smoothScroll">{{$loop->iteration}}</a></li>

    @endforeach

</ul>

if one option from 1st question is checked the class="smoothScroll" must changed to class="SmoothScroll green" like this the question list must change. but only once. that is if only one check box from each question only.

what i tried in my JS is

 var questions = document.getElementsByName('questions[]');

here whats my problem is questions1, questions2.. like this the class name and id changes based question


            var chkboxx = document.getElementsByName('stud_answer[]');

here whats my problem is stud_answer1, stud_answer2.. like this the class name and id changes based question

for (var i = 1; i < questions.length; i++) {
    var chkboxx = document.getElementsByName('stud_answer' + [i] + '[]');
    alert(i);
    if (chkboxx[i].is(':checked')) {
        alert("s");
        alert("checked");
    } else {
        alert("notchecked");
    }
}

how i can iterate and check if a checkbox from specifc question is checked or not, kindly some one help please

0 likes
2 replies
jlrdw's avatar

With check boxes if none is checked nothing is passed. If you have a value then at least one checkbox was checked.

AbdulBazith's avatar

@jlrdw thank you so much for your response. actually i have some javascript work so i faced this problem.

i cleared that some what.

can you kindly answer for these please. really sticking into it....

Link1:

https://laracasts.com/discuss/channels/eloquent/check-whether-student-wrote-the-exam-from-two-relationship-table

Link2:

https://laracasts.com/discuss/channels/eloquent/multiple-foreign-key-indicating-one-primary-key-and-getting-values-from-table

Please or to participate in this conversation.