View Full Version : Table Search through Java Script
gayaneducation
Dec 18th 2008, 7:32 pm
I need Java Script function for search data(text) inside a html table. not a my sql table.
ex: inside a table "<table><tr><td>some text here</td></tr></table>" i have several text. i need Java Script function to search text inside the table with highlighting it.
or can u pls suggest the way of finding the text in a html table.
please help to solve this issue
lp1051
Dec 19th 2008, 5:39 am
Hi,
non-standard, but working, could be :
var tbl=document.getElementsByTagName('table')[0]; //in the case there's only one table on the page
var td=tbl.getElementsByTagName('td');
var l=td.length;
for(var i=0; i<l; i++) alert('cell content is: '+td[i].innerHTML);
Be careful to run it with alert on big table
Or you can use W3C way using rows, cells arrays - the same principle
DuncanM
Dec 20th 2008, 3:55 pm
Is the table fixed size?
Have you explicitly named the text boxes?
Usually, what I like to do is define a variable and assign it the value of the dataForm.elements value:
i.e.
var dataFormElements = dataForm.elements;
That way, I avoid having to repeatedly re-resolve this quantity each time I use it later in the code.
If you don't know the number of text boxes in your form, or don't want to bother writing it out explicitly, you could use the length attribute.
i.e.
var vecDim = dataForm.elements.length;
Now vecDim is the number of entities in the form, assuming that is all your form contains.
You could then loop through all the elements of the form and extract the desired information from it:
for (var i = 0; i < vecDim; i++) {
//Extract the desired information
}
Alternately, if you have only a few table elements, and you have explicitly named them, you could just explicitly extract the information from each one.
i.e.
b[0] = parseFloat(dataForm.b1.value);
b[1] = parseFloat(dataForm.b2.value);
b[2] = parseFloat(dataForm.b3.value);
b[3] = parseFloat(dataForm.b4.value);
(This example uses floats, but it could easily be altered for whatever data type you are using.)
rene7705
Dec 21st 2008, 10:43 pm
If you're gonna do a lot of different types of these lookups, i'd recommend a base-lib like jQuery.com to ease development and increase code readability..
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.