Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2006
    Location
    USA
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Writing out a searched array

    Okay, here we go. I am building a JavaScript site search [must-be client side]. The code works very well except that it only writes out the last item returned from the search array not ALL the items returned in the search.

    The magic happens [or not] at the win.innerHTML = searchopen + line + searchclose;

    I need to get the 'line' variable to write out all the items returned not just the last one.

    Help!

    The array is huge so I left it off to save space.

    Code:
    searchopen = "<table border='0' cellspacing='2' cellpadding='5' width='100%' bgcolor='#f0f8ff' style='border:1px solid #000066;'><tr><td><h3 style='margin:0px; padding:0px;'>Search Results:</h3></td><td align='right' valign='top'>[<a href='javascript:void(0);' onclick='document.getElementById(\"searchDiv\").innerHTML=\"\"' class='copyright'><u>close search</u></a>]";
     
    function search(frm){
    //win = window.open("","search","scrollbars,width=500,height=500,left=10,top=10");
    var win = document.getElementById("searchDiv");
    frm = document.searchform.srchval.value;
    txt = frm.split(" ");
    fnd = new Array(); total=0;
    for (i = 0; i < item.length; i++){
    fnd[i] = 0; order = new Array(0, 4, 2, 3);
    for (j = 0; j < order.length; j++)
    for (k = 0; k < txt.length; k++)
     
    if (item[i][order[j]].toLowerCase().indexOf(txt[k]) > -1 && txt[k] != "")
    fnd[i] += (j+1);
    }
    for (i = 0; i < fnd.length; i++){
    n = 0; w = -1;
    for (j = 0;j < fnd.length; j++)
    if (fnd[j] > n){
    n = fnd[j];
    w = j;
    };
    if (w > -1) total += show(w, win, n);
    fnd[w] = 0;
    }
    searchclose = "<tr><td align='right' colspan='2'>Total found: " + total + "<br></td></tr></table>";
    win.innerHTML = searchopen + line + searchclose;
    }
     
    function show(which,wind,num) {
    link = item[which][1] + item[which][0]; 
    line = "<tr><td colspan='2'><b><a href='" + link + "' style='text-decoration:underline;' target='parent'>" + item[which][2] + "</a></b> &nbsp; &nbsp; Score: " + num + "<br>";
    line += item[which][3] + "</td></tr>"; 
    //alert(result);
    return 1;
    }
     
    document.body.appendChild(win);
    • Jesus called. He says He misses you.

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just glancing at it I suggest formally making line a global variable and setting it to an empty string. Then on assignment to line make it '+=' instead of just '='. My take on your code is it overwrites line every time you call show().

    Code:
    var line = "";
    
    function show(which,wind,num) {
      link = item[which][1] + item[which][0]; 
      line += "<tr><td colspan='2'><b><a href='" + link + "' style='text-decoration:underline;' target='parent'>" + item[which][2] + "</a></b> &nbsp; &nbsp; Score: " + num + "<br>";
      line += item[which][3] + "</td></tr>"; 
      //alert(result);
      return 1;
    }
    dvaid_kw

  • #3
    New Coder
    Join Date
    Sep 2006
    Location
    USA
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Let it be known that david_kw has the eyes of an eagle and generally ROCKS!

    Thanks a bunch. Even though it was a fairly simple fix I would have never have gotten that. I was stumbling down the wrong path entirely.
    • Jesus called. He says He misses you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •