How to refresh div with table after clicking on button?



I have created an input search and when the search the button is clicked, I use just javaScript (no jQuery or other javaScript library) to make an asynchronous ajax call to my Web API, passing the user input as a parameter along with the call. When the ajax response comes back, I dynamically build a HTML table that shows the data from the ajax response.

I got everything to work except whenever I click the search button, another table is made under the previous search results. How do I make it so that every time I search a new item, the previous search gets replaced with the new search?

Here is my html code:

<!DOCTYPE html>
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
        <title>Anthracite's Products</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <script src=""></script>
        <link href="" rel="stylesheet">
        <link href="style.css" rel="stylesheet">
        <div ng-app="">
            <div ng-include src="'top.html'"> </div>
            <div id="content">
                <h1>Products & Coffee</h1>

                    Enter product name or starting letter for the product/coffee you want to find:

                <input type="text" id="productSearch" value="A" />
                <input type="button" value="Search" onClick="getProduct()"/>
                <div id="databaseError"></div>
                <div id="productTable"></div>
                <div ng-include src="'bottom.html'"> </div>

            <script src="ajaxFw.js"></script>
            <script language="Javascript" type="text/javascript">
                    "use strict";

                    function $(element) {
                        return document.getElementById(element);

                    function getProduct() {
                        var url = "webApi.jsp";
                        url += "?q=" + $("productSearch").value;

                        function success(jsonObject) {
                            if (jsonObject.product === null) { // means artist not found.
                                $("productTable").innerHTML = "Error: JSON string evaluated to null.";
                            if (jsonObject.recordList.length === 0) {
                                $("productTable").innerHTML = "No Products Match Your Search";
                            } else {
                                var newRow;
                                var newCell;
                                var newImg;

                                var newTable = document.createElement("table");

                                for (var i in jsonObject.recordList) {
                                    newRow = newTable.insertRow(i);
                                    newCell = newRow.insertCell(0);
                                    newImg = document.createElement("img");
                                    newImg.src = jsonObject.recordList[i].imageURL;

                                    newCell = newRow.insertCell(1);
                                    newCell.innerHTML = jsonObject.recordList[i].description;

                                    newCell = newRow.insertCell(2);
                                    newCell.innerHTML = jsonObject.recordList[i].price;

                                var newHead = newTable.createTHead();
                                newRow = newHead.insertRow(0);

                                newCell = newRow.insertCell(0);
                                newCell.innerHTML = "Product";

                                newCell = newRow.insertCell(1);
                                newCell.innerHTML = "Description";

                                newCell = newRow.insertCell(2);
                                newCell.innerHTML = "Price";

                        function error(httpReqObject) {
                            alert("there was an error");
                            console.log(httpReqObject); // when you console.log an object
                            // you can click in the log to discover all its property values.
                        ajaxCall(url, success, error);

Here is my ajaxFw.js:

function ajaxCall(url, successFn, errorFn) {
    var httpReq;
    if (window.XMLHttpRequest) {
        httpReq = new XMLHttpRequest();  //For Firefox, Safari, Opera
    } else if (window.ActiveXObject) {
        httpReq = new ActiveXObject("Microsoft.XMLHTTP");   //For IE 5+
    } else {
        alert('ajax not supported');

    function sendRequest() {
        console.log("ajax calling "+url);
        cache: false;"GET", url);
        httpReq.onreadystatechange = handleResponse;
    } // sendRequest

    function handleResponse() {
        if (httpReq.readyState === 4) {
            if (httpReq.status === 200) {
                console.log("Ajax success: response text is "+httpReq.responseText);
                var json = "(" + httpReq.responseText + ")";
                var object = eval(json);
            } else {
                console.log("Ajax error: "+httpReq.statusText);
                errorFn("Could not read " + url + ". Status is "+httpReq.statusText);
    } // handleResponse