Filter HTML Table with JavaScript

This code can be used to hide/show rows of a table based upon user input.

function filter (term, _id, cellNr){
	var suche = term.value.toLowerCase();
	var table = document.getElementById(_id);
	var ele;
	for (var r = 1; r < table.rows.length; r++){
		ele = table.rows[r].cells[cellNr].innerHTML.replace(/<[^>]+>/g,"");
		if (ele.toLowerCase().indexOf(suche)>=0 )
			table.rows[r].style.display = '';
		else table.rows[r].style.display = 'none';
	}
}

This function searches in the table with the id defined by _id in every row in the cell defined by cellNr. The search is case insensitive.
The usage is straightforward:

<form>
	<input name="filter" onkeyup="filter(this, 'sf', 1)" type="text">
</form>

It should work with all modern browsers, e.g. IE5, Firefox 1.0, Opera 7 and Mozilla 1.0.
 

Posted by:

Comments History


by: on
Great ..... It is working fine.
 

Name :  

Email :  

Comment Below