Question

I am using below code to refresh only one DIV in the html page

<script type="text/javascript">
    $('.active').live('click', function(event) {
        var elem = $(this);
        var url = $(this).attr('href');
        alert(url);
        $.ajax({
            url: url,
            success: function(result) {
                var dtr = $("#test", result);
                $('#test').html(dtr);}
        });
        event.preventDefault();
        event.stopPropagation();
    });
</script>

But it is not loading the other JS like below code which I wrote in the same html page and worked when page is loaded or refreshing.

<script type="text/javascript" charset="utf-8">
    $(document).ready( function () {
        $('#example').dataTable( {
            "sDom": 'T<"clear">lfrtip',
            "oTableTools": {
                "sScrollY": 200,
                "sScrollX": "100%",
                "sScrollXInner": "110%",
                "sSwfPath": "../mymedia/savefiles/copy_csv_xls_pdf.swf"
            }
        } )
        .columnFilter({
            aoColumns: {{columnfilters|safe}}
        });
    } );
</script>

Suggest me how can I call or refresh the JS automatically when the DIV is loaded or refreshed with AJAX.

Was it helpful?

Solution

Try this

Initially you initialized the table so first clear that table

$('#example').dataTable().fnDestroy();

Then initialize again after ajax success

$('#example').dataTable();

Like this

<script type="text/javascript"> 
$('.active').live('click', function(event) {
$('#example').dataTable().fnDestroy(); 
var elem = $(this); 
var url = $(this).attr('href'); alert(url); 
$.ajax({ 
url: url, 
success: function(result) 
{ 
var dtr = $("#test", result); 
$('#test').html(dtr);
$('#example').dataTable( {
                "sDom": 'T<"clear">lfrtip',
                "oTableTools": {
                    "sScrollY": 200,
                    "sScrollX": "100%",
                    "sScrollXInner": "110%",
                    "sSwfPath": "../mymedia/savefiles/copy_csv_xls_pdf.swf"
                }
            } )
            .columnFilter({
        aoColumns: {{columnfilters|safe}}
    });
} 
});
event.preventDefault(); 
event.stopPropagation();
}); 
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top