0

Hello every one this my dynamic form in this I am using select2 search option in this form and it is not working

this Head

         <script src="jquery-3.3.1.min.js" type="text/javascript"></script>

    <!-- Select2 JS -->
    <script src="select2/dist/js/select2.min.js" type="text/javascript"></script>


And this is my dynamic html Form i was not able to write full code due to stack overflow restrictions

  html += `<td><select type="text" name="head[]" class="form-control inst_amount"><option value="">Select Name</option><?php $sql=mysqli_query($con,"SELECT * FROM bank_details order by name");
while($row=mysqli_fetch_array($sql))
{?><option value="<?php echo htmlentities($row['id']);?>"><?php echo htmlentities($row['Name']);?></option><?php }?></select></td>`;
  

This is select2 library


    <!-- Select2 JS -->
    <script src="select2/dist/js/select2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function(){

        $('.inst_amount').select2();
   });

    </script>

Thimothi
  • 35
  • 5
  • any error in console you are facing? – mehmood khan Feb 03 '21 at 10:10
  • @mehmoodkhan no error just normal select option is coming – Thimothi Feb 03 '21 at 10:15
  • Assuming by 'dynamic form' you mean that it's loaded via AJAX, then you need to invoke `select2()` on the select elements which you create in the AJAX callback – Rory McCrossan Feb 03 '21 at 10:17
  • Just before `$('.inst_amount').select2();` add `console.log($('.inst_amount').length)` - what's the output? If it's 0/zero then that's your issue, the element doesn't exist at the time you make the call. The term "dynamic html" seems to be being misused recently (to just mean server-side html) - so please confirm that you're loading your form via ajax – freedomn-m Feb 03 '21 at 10:28
  • @freedomn-m yes sir i am loading form via AJAX – Thimothi Feb 03 '21 at 10:48
  • 1
    In that case the issue is as I described in my previous comment. See the duplicate for an example of how to address the issue. – Rory McCrossan Feb 03 '21 at 11:12

1 Answers1

-1

Guess you need to add the class "inst_amount" to your select element.