javascript - Why my JS code is not working if I called my JS function inside PHP?
one text
Solution:
<?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
Source