why the js code doesn’t work when i call my js function inside php?

<?php
 echo "<select name='choose' id='choose' class='choose' onclick='selectfun()'> 
         <option value='radio' onclick='radio()'>RADIO</option>
         <option value='checkbox'>CHECK BOX</option>
         <option value='email'>EMAIL</option>
         <option value='file'>FILE</option>
         <option value='tel'>TELEPHONE</option></select>";
?>


 <script>
  function selectfun() {
  // initializing the select tag to the variable selectBox
  var selectBox = document.getElementById("choose");
  // onclick reading the option value by select tag index
  var selectedValue = selectBox.options[selectBox.selectedIndex].value;
   // checking the selected value is `radio` or not
   if(selectedValue == 'radio'){
   // if selected value is radio then alert the selected value
    alert(selectedValue);
   }
  }
  </script>

you can use echo of PHP to print the HTML code, please check my above code that may give you some idea how to do it. and it’s easy to bind onclick event to the select tag instead of an option, reference here

here is the working fiddle

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top