I\'m trying to filter a table from an alphabetical input with jQuery.
I have first and last names in two columns of the table, and I\'d like
Try this. If necessary substitute y with $(y).
$('tr').hide();
$('select').change( function(){
var letter = $(this).val();
var dataset = $('#tableID').find('td');
$.each(dataset, function(x, y){
if( y.substr(0,1) == letter){
y.parent().show();
}
});
});
Edit
@SolutionYogi. You are right. I guess this line can be rewriten as:
var dataset = $('#tableID').find('tr').children('td').slice(0,1);
Never tried that though.
EDIT 2
I tested this. I hope is elegant enough as well and I doesn't have more errors.
$('tr').hide();
$('select').change( function(){
var letter = $(this).val();
var dataset = $('#tableID').find('tr');
$.each(dataset, function(x, y){
var data = $(y).children().slice(0,2);
$.each(data, function(a, b){
if( $(b).html().substr(0,2) == letter){
$(b).parent().show();
}
});
});
});