0

I have this javascript function to set radio button to checked state with in php

I call this javascript function using php echo

I tried placing javascript everywhere but still php doesn't set the radio button checked

This is my php with javascript

                     <div class="row text-center">
                             <script type="text/javascript">

                                function checkRadio(serviceid) {
                                   $('#radio' + serviceid).prop('checked', true);
                                 }
                                </script>

                              <?php echo "<script type='text/javascript'>checkRadio({$row['serviceid']});</script>"; ?>

                             <div class="col-md-3 col-sm-6 col-6 ad-image">
                             <input type="radio" name="radios" id="radio1" value="1" class="input-hidden" />
                             <label for="radio1">
                                                        <img src="http://placehold.it/70" alt="I'm radio" />
                            </label>
                        </div>

                      <div class="col-md-3 col-sm-6 col-6 ad-image">
                        <input type="radio" name="radios" id="radio2" value="2" class="input-hidden" />
                           <label for="radio2">
                              <img src="http://placehold.it/70" alt="I'm radio" />
                            </label>
                        </div>
          </div>

$row['serviceid'] which is in a while loop (which runs only one time) and it carries values 1 (e.i $row['serviceid'] = 1)

I tried placing alert(serviceid) and it works fine.

But it doesn't set radio1 to set state

Can someone help me?

1 Answers1

0

Original answer can be found here: How to set radio button status with JavaScript

If you just want to set radio button status to checked you really don't need jQuery, you can simply use vanilla javascript:


    function checkRadio(serviceid) {
        radiobtn = document.getElementById("radio" + serviceid);
        radiobtn.checked = true;
    }

Krunx
  • 226
  • 1
  • 7