Question

I generate an html table from a .csv using php.

I got this JavaScript Livesearch that works pretty decent, I use it to search the table.

This is the code:

function doSearch() {
    var searchText = document.getElementById('searchTerm').value;
    var targetTable = document.getElementById('dataTable');
    var targetTableColCount;


    //Loop through table rows
    for (var rowIndex = 0; rowIndex < targetTable.rows.length; rowIndex++) {
        var rowData = '';

        //Get column count from header row
        if (rowIndex == 0) {
            targetTableColCount = targetTable.rows.item(rowIndex).cells.length;
            continue; //do not execute further code for header row.
        }

        //Process data rows. (rowIndex >= 1)
        for (var colIndex = 0; colIndex < targetTableColCount; colIndex++) {
            rowData += targetTable.rows.item(rowIndex).cells.item(colIndex).textContent;
        }

        //If search term is not found in row data
        //then hide the row, else show
        if (rowData.indexOf(searchText) == -1)
            targetTable.rows.item(rowIndex).style.display = 'none';
        else
            targetTable.rows.item(rowIndex).style.display = 'table-row';
    }

    function capitaliseFirstLetter(string)
    {
        return string.charAt(0).toUpperCase() + string.slice(1);
    }
}

The search is case sensitive about capital letters, I would like it to ignore that, so that it will find the entries anyways.

If I search for "jon doe" I want to find: "jon doe", "Jon doe", "Jon Doe", "JON DOE", etc.

How can this be implemented into my existing code?

Was it helpful?

Solution

You could do ToUpperCase to compare strings for example:

var myName = 'Jon Doe';

Then you'd do the same for whatever name you're checking for

var searchName = GetElementById("MyTextbox");

var areEqual = myName.toUpperCase() === searchName.toUpperCase();

Then you can do

if(areEqual == True) {
    document.write("Names Match");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top