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
  • 32,412
  • 7
  • 33
  • 72
user2715898
  • 781
  • 3
  • 9
  • 19

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
  • 64,240
  • 8
  • 67
  • 88
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,525
  • 4
  • 19
  • 34
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
  • 425
  • 2
  • 10