Table Search through Java Script

Discussion in 'JavaScript' started by gayaneducation, Dec 18, 2008.

  1. #1
    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
     
    gayaneducation, Dec 18, 2008 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    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.innerHTML);
    Be careful to run it with alert on big table

    Or you can use W3C way using rows, cells arrays - the same principle
     
    lp1051, Dec 19, 2008 IP
  3. DuncanM

    DuncanM Well-Known Member

    Messages:
    136
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #3
    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; 
    Code (markup):
    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;
    Code (markup):
    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
    }
    
    Code (markup):
    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);
    
    Code (markup):
    (This example uses floats, but it could easily be altered for whatever data type you are using.)
     
    DuncanM, Dec 20, 2008 IP
  4. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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..
     
    rene7705, Dec 21, 2008 IP