user3304642
user3304642

Reputation: 191

How to use multiselect filter with ajax

Hi all I have working multiselect filter downloaded from this source http://www.erichynds.com/, I am trying to use this with Ajax, though my ajax function is working, and showing html generated by php in window.alert(html), but multiselect fileter has no effect, I really don't know how to solve it. this is what I have done so far

HTML

 <table>
  <tr>
  <td>
  <select id='pr_select' name='prj' onChange='show("pr_select","output1");' >
      <option value='28'>28</option>
      <option value='29'>29</option>
      <option value='30'>30</option>
  </select>
  </td>
  </tr>
  <tr>
  <td>
  <div id='output1'></div></td>
  </tr>
  </table>

JAVASCRIPT

<script>

  function show(sel,id) {

    var selected = $("#"+sel).val();  
    $("#"+id).html( "" );

    if (selected.length > 0 ) { 

     $.ajax({
            type: "GET",
            url: "get_data.php",
            data: "select="+selected,
            cache: false,
            beforeSend: function () { 
                $("#"+id).html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) { 
                // Ajax is success but multiselect is not working.....   
                window.alert(html),
                $("#"+id).html( html );

            }

        });
    } 

  }

  $(document).ready(function(){
     $("#test").multiselect();
  });

  </script>

Output generated in ajax success block -window.alert

  <select id='test' name='multiple_data[]' multiple='multiple'>
  <option value='USA'>USA</option>
  <option value='UK'>UK</option>
  </select>

I even tried for output1 division also like this no luck

 $(document).ready(function(){
     $("#output1").multiselect();
  });

Upvotes: 2

Views: 1548

Answers (2)

Akshay Hegde
Akshay Hegde

Reputation: 16997

This would solve your problem try

success: function(html){    
        document.getElementById(id).innerHTML = html;
        $("#"+id).multiselect().multiselect('refresh').multiselectfilter();
      },

Upvotes: 1

Jai
Jai

Reputation: 74738

Try not to bind the method on doc ready instead apply in the complete method of ajax:

<script>

  function show(sel,id) {

    var selected = $("#"+sel).val();  
    $("#"+id).html( "" );

    if (selected.length > 0 ) { 

     $.ajax({
        .......
            success: function(html) { 
                // Ajax is success but multiselect is not working.....   
                window.alert(html),
                $("#"+id).html( html );

            },
            complete:function(){
                   $("#test").multiselect(); // add it here in the ajax
            }

        });
    } 

  }
  </script>

Upvotes: 1

Related Questions