Rohitashv Singhal
Rohitashv Singhal

Reputation: 4557

Unable to delete a previous array in Javascript

I am going to design a live search engine, taking the array from a database. When I am running this code, it is giving the current results with the previous result. I want to remove the previous results. Here is my code in Javascript.

var searchTable=document.getElementById('searchResults');<br>
var tbody=searchTable.getElementsByTagName('tbody');
var queryResult=http.responseText;
var keyArray=queryResult.split("|");
keyArray.shift();
var arrayLength=keyArray.length;
//console.log(keyArray);
var y=document.getElementsByTagName('tbody')[0];
for(var i=0;i<arrayLength;i++) {
    var trEl=document.createElement("tr");
    var tdEl=document.createElement("td");
    var trtdEl=trEl.appendChild(tdEl);
    var textNode=document.createTextNode(keyArray[i]);
    trtdEl.appendChild(textNode);
    y.insertBefore(trEl,y.firstChild);
}

Please help me guys..

Upvotes: 0

Views: 83

Answers (1)

MaxArt
MaxArt

Reputation: 22637

Before the for loop, you must remove all the children of the element y. Put something like this:

while (y.firstChild) y.removeChild(y.firstChild);

Upvotes: 1

Related Questions