Note: Multiple select tags were added using for loop in PHP.The problem I am facing is when I have multiple select tags with same options I could not know which select is calling selectfun()
<?php
$dynamicTable= ""; for ($x = 1; $x <= $a; $x++) { $dynamicTable .= "<tr><td>"; $dynamicTable .= $x; $dynamicTable .= "</td><td><input type='text'></td> <td><input type='text'></td> <td> <select name='choose' class='choose' onclick='selectfun()'> <select name='choose' class='choose' onclick='selectfun()'> <option value='text'>VAR CHAR</option> <option value='number'>NUMBER</option> <option value='radio'>RADIO</option> </select> ?> } echo $dynamicTable; <script> function selectfun() { var selectBox = document.querySelectorAll(".choose")[?]; var selectedValue = selectBox.options[selectBox.selectedIndex].value; if (selectedValue == 'radio') { alert("Hi"); } } </script>
Advertisement
Answer
function selectfun(elem){ if(elem.value == 'radio'){ alert(elem.value); } else{ alert('it is not radio') } }
<select name='choose' class='choose' onclick='selectfun(this)'> <option value='text'>VAR CHAR</option> <option value='number'>NUMBER</option> <option value='radio'>RADIO</option> </select> <select name='choose' class='choose' onclick='selectfun(this)'> <option value='text'>VAR CHAR</option> <option value='number'>NUMBER</option> <option value='radio'>RADIO</option> </select>
you can use pass parameter
from the click event, here you can read more about how to pass the parameter from the functions in JS.