4:59 pm - Friday May 24, 2013

Simple Table Search in Javascript

Table search a sim­ple way to find data in html table rows

Here is the func­tion for sim­ple table search in javascript.

function filterSearch(val,tId) {
	try{

	var q = document.getElementById(val);
	var v = q.value.toLowerCase();
	var tblFilter= document.getElementById(tId);
	var rows = tblFilter.getElementsByTagName('tr');
	if(rows){
	var on = 0;
	for ( var i = 0; i < rows.length; i++ ) {
		var fullname = rows[i].getElementsByTagName('td');
		fullname = fullname[0].innerHTML.toLowerCase();
		if ( fullname ) {
			if ( v.length == 0 || (v.length < 1 && fullname.indexOf(v) == 0) || (v.length >= 1 && (fullname.substring(0, v.length)).indexOf(v) > -1 ) ) {
				rows[i].style.display = '';
				on++;
			} else {
				rows[i].style.display = 'none';
			}
		}
	}
	}
	var n = document.getElementById('noresults');
	if ( on == 0 && n) {
		n.style.display = '';
		document.getElementById('qt').innerHTML = q.value;
	} else {
		n.style.display = 'none';
	}
	}catch(e){}

}

Down­load Work­ing example

You May Also Like:

Filed in: Html, Javascript

One Response to “Simple Table Search in Javascript”

  1. bharani Kumar
    August 12, 2012 at 10:08 pm #

    Hi Murali.

    Nice arti­cle .

    How to cre­ate a dynamic table .
    rows should be cre­ated at the time of data pop­u­lat­ing could you please explain that.

Leave a Reply