Set focus to the Search Box
Set focus to the Search Box
Hello, I searched the entire forum and although I found information, I can not understand where the code is placed to focus the search box. (Sorry for my English) Here my code:
$(document).ready(function() {
$('#example').DataTable( {
initComplete: function () {
this.api().columns([1]).every( function () {
var column = this;
var select = $('<select><option value=""></option></select>')
.appendTo( $(column.header()).empty() )
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
} );
} );
}
} );
} );
Thank you.
Answers
Could it be that this is not working because my table is extremely large? I tried everything.
This not work in my case: $('#example_filter input').focus();
I already found the solution:
$(document).ready(function() {
setTimeout(function () { $('div.dataTables_filter input').focus(); }, 5000);
$('#example').DataTable( {
initComplete: function () {
this.api().columns([1]).every( function () {
var column = this;
var select = $('<select><option value=""></option></select>')
.appendTo( $(column.header()).empty() )
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
} );
} );
}
} );
} );