0

The two functions in the javascript are both having a statement-

$('#'+key).html(data) 

funtion data(data) {
  $('#'+key).html(data)
}

funtion data2(data) {
  $('#'+key).html(data)
}

which is essentially replacing the value of the key. I don't want the replacement but basically add to the same data and then render the new value.

I am very new to Javascript- will be glad if someone can point in the right direction. Thanks in advance.

Ele
  • 33,468
  • 7
  • 37
  • 75
user2715898
  • 921
  • 3
  • 13
  • 20

3 Answers3

2

I don't want the replacement but basically add to the same data

Use append instead of html

$('#'+key).html(data) 

funtion data(data) {
  $('#'+key).append(data)
}

funtion data2(data) {
  $('#'+key).append(data)
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
1

Without JQuery:

If you want to add just text, use appendChild:

function addText(divID, text) {
    document.querySelector("#"+divID)
        .appendChild(document.createTextNode(text));

}
/** TEST **/
var number = 1;
var intervalID = setInterval(addMore, 500);
function addMore() {
    addText("appendHere", "Text #"+(++number)+" ")
    // do not add infinitelly
    if(intervalID>20)
        clearInterval(intervalID);
}
<div id="appendHere"></div>

If you want to append HTML, just alter the code a bit:

function addHTML(divID, html) {
    var fragment = document.createElement("span");
    fragment.innerHTML = html;

    document.querySelector("#"+divID)
        .appendChild(fragment);

}
Giulio Bambini
  • 4,695
  • 4
  • 21
  • 36
1

As per first comment of @gurvinder372

funtion data(data) {
  $('#'+key).append(data)
}

funtion data2(data) {
  $('#'+key).append(data)
}

This will help you to understand data handling on HTML elements using JQuery methods.

NnN
  • 463
  • 2
  • 11