I’ve been getting help trying to make this search function work the way it should but I can’t seem to get it that last little bit that would make it perfect. Basically, it allows the user to search in a table for corresponding results and then hides all rows that don’t privde a match. So far it works perfectly, minus one tiny little flaw I’m not willing to accept. It searches all of the text in each row. The reason this is a problem is I’m searching for keywords that are likely show up in places that will provide false results.
For instance in the current working example if you run a search, if the term shows up in the body of text it comes back as a positive. What if I had the word “web” in a print project? False positive, seem my problem?
I have a solution that would suite me just perfectly, the problem is I don’t know how to modify the code to make this work. If I could limit the search to just any element with the class of “tags” that would be enough. As I plan on keywording every project I put on the page it will provide a good solution to the problem until a better system can be found. Can anyone help me out? The class will only be applied to paragraphs.
function dog( id )
{
return document.getElementById( id );
}
function filter ()
{
var word_filter_arr = dog('phrase_filter').value.split(' ');
var year_filter = dog('year_filter').value;
var category_filter = dog('category_filter').value;
var table = dog('result_table');
var row_content;
var element_filtered = false;
var word_found = false;
var element_visible = false;
for (var r = 1; r < table.rows.length; r++)
{
row_content = table.rows[r].innerHTML.replace(/<[^>]+>/g,"");
element_visible = false;
if( word_filter_arr.length == 0 )
{
word_found = true;
}
for ( var i = 0; i < word_filter_arr.length; i++ )
{
if ( row_content.toLowerCase().indexOf( word_filter_arr* ) >=0 )
{
word_found = true;
}
}
if( word_found && ( row_content.toLowerCase().indexOf( year_filter ) >=0 || year_filter == -1 ) )
{
if( word_found && ( row_content.toLowerCase().indexOf( category_filter ) >=0 || category_filter == -1 ) )
{
element_visible = true;
}
}
if( element_visible )
{
table.rows[r].style.display = 'block';
}
else
{
table.rows[r].style.display = 'none';
}
}
}