I am trying to highlight( or style ) the search term that a user will type. Right now this styles the entire table cell. I want just the search term highlighted and not the entire td cell.
I am showing the table rows that contain the keyword, but cannot seem to style the "searched" keyword which is jQuery('#txtSearch').val() within the results...
This is what I have
jQuery('#' + view + ' tr td:containsNoCase(\'' + jQuery('#txtSearch').val() + '\')').css( "text-decoration", "underline" );
Thx
more complete code...
if (jQuery('#txtSearch').val().length > 2) {
jQuery('#' + view + ' tr').hide(); // hide all rows
jQuery('#' + view + ' tr:first').show(); // // show the header row
jQuery('#' + view + ' tr td:containsNoCase(\'' + jQuery('#txtSearch').val() + '\')').parent().show(); // show the matching rows (using the containsNoCase)
jQuery('#' + view + ' tr td:containsNoCase(\'' + jQuery('#txtSearch').val() + '\')').css( "text-decoration", "underline" ); // highlight searchterm
// THIS IS THE ANSWER
function highlightOnly(text) {
$('#' + view + ' td').each(function (i, e) {
var $e = $(e);
$e.html($(e).html().split(text).join('<span class="matching">' + text + '</span>'));
})
}
$('.matching').replaceWith(function () {
return this.innerText;
});
highlightOnly($('#txtSearch').val().toUpperCase());
// THIS IS THE ANSWER
jQuery('#' + view + ' tr:nth-of-type(odd)').css('background', '#fff'); // chg bg
}