Steve Steve - 3 months ago 11
jQuery Question

Datatable searching the clicked element

I have used datatable for a table.

$('#tableid').dataTable({
"aLengthMenu": [
[2, 5, 7, -1],
[2, 5, 7, "All"]
],
"iDisplayLength": 5
});


and I if click on any div element I want the value of the div element to be searched in the datatable.e.g.

<div id="search>
<a href="">this</a>
</div>


If I click on this, 'this' to be searched in the table.How can this be achieved?

$(document).on('click','#search',function(e)
{
e.preventDefault();
var search=$('#search').val();

}

Answer

Use search() function for datatable to search manually

<script>
        var table = $('#tableid').dataTable({
          "aLengthMenu": [
            [2, 5, 7, -1],
            [2, 5, 7, "All"]
          ],
          "iDisplayLength": 5
        });

$(document).on('click','#search',function(e)
    {
        e.preventDefault();
        var search =$('#search').text();
        table.search(search).draw();
    }   
</script>

     <div id="search>

     </div>
Comments