1

I have a foreach loop that generates an input with type of checkbox with items (from DB). Right now, there is no default checkbox that is checked and every time a user checks the checkbox it calls to JS function.

Now, the clients wants me to check the first checkbox by default. I know how to check it but how do I call the JS function by default?

This is my HTML - the update_addon() is JS func.

<? foreach ($pros as $pro):?>
    <label class="checkbox text-left">
        <input type="checkbox" name="checkbox" 
         onchange="update_addon(<?=$pro['id']?>,this)">
         <?=$pro['name']?> ( <?=$pro['price']?> )
    </label>
<? endforeach; ?>

The update_addon() is an AJAX call.

2 Answers 2

1

You have a syntax problem. You must put question mark on this part "<?=$pro['id']>".

 <? foreach ($pros as $pro):?>
     <label class="checkbox text-left">
         <input type="checkbox" name="checkbox" 
          onchange="update_addon(<?=$pro['id']?>,this)">
          <?=$pro['name']?> ( <?=$pro['price']?> )
      </label>
  <? endforeach; ?>
Sign up to request clarification or add additional context in comments.

2 Comments

while the code may solve the problem, update this answer with required details like where is the problem, what is the problem, etc.
It is just copy-past. The code works fine, now I need to change it a bit to fit the new situation
0

Finally, I did it. did a click() event on js file to click the first checkbox. thanx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.