3

I am currently studying Javascript (and AJAX) for Web Development and found out I can either use textContent or appendChild(newlyCreatedTextNode) to add text to an element node. The question is: what is the difference between modifying the textContent of an Element Node and creating a new Text Node and then appending it to the Element Node?

(xhr is an XMLHttpRequest object)

Here is how I was doing the code:

xhr.onload = function() {
   if (this.status === 200) {
        // companies is an array of objects
        let companies = JSON.parse(this.responseText);
        // retailCompanies is an array of objects
        let retailCompanies = companies.filter(company => company.category === "Retail");
        // h1 with textContent of "Retail Companies:"
        let h1Node = document.createElement("h1");
        h1Node.textContent = "Retails Companies:";
        document.body.appendChild(h1Node);
        // Main ul
        let ulNode = document.createElement("ul");
        for (let company of retailCompanies) {
            let liNode = document.createElement("li");
            /* Code to be analyz(s)ed below*/
            let textLi = document.createTextNode(company.name);
            liNode.appendChild(textLi);
            /* Code to be analyz(s)ed above */
            ulNode.appendChild(liNode);
            document.body.append(ulNode);
        }
    } else {
        console.log(`Returned error: ${this.status}`);
    }
}

But found out that I could simply do this:

xhr.onload = function() {
    if (this.status === 200) {
    // companies is an array of objects
        let companies = JSON.parse(this.responseText);
        // retailCompanies is an array of objects
        let retailCompanies = companies.filter(company => company.category === "Retail");
        // h1 with textContent of "Retail Companies:"
        let h1Node = document.createElement("h1");
        h1Node.textContent = "Retails Companies:";
        document.body.appendChild(h1Node);
        // Main ul
        let ulNode = document.createElement("ul");
        for (let company of retailCompanies) {
            let liNode = document.createElement("li");
            /* Code to be analyz(s)ed below */
            liNode.textContent = company.name;
            /* Code to be analyz(s)ed above */
            ulNode.appendChild(liNode);
            document.body.append(ulNode);
        }
    } else {
        console.log(`Returned error: ${this.status}`);
    }
}

Both work fine, but I wonder what is the difference (if there is any - I suppose so). I imagine that, when creating a new Element Node, it already comes with a Text Node (hence why I can use the textContent property, most likely), but why then have a createTextNode() method?

Sorry for any mistakes or if this question is (somehow) obvious, just really couldn't find anything about this and am very new to working with the DOM.

Thanks for any help with this!

(Also, I now thought of using forEach for the code, but really doesn't matter for this question. Love if you help me improve my code in any way, but please focus on the question, if possible :) )

Daniel A.
  • 63
  • 4
  • 1
    Here both have the same effect, but in general the difference is that appending will append, and setting textContent will overwrite. So doing it twice to a single `
  • ` will give different results.
  • –  Sep 01 '19 at 21:53
  • 1
    Btw, you can move `document.body.append(ulNode);` out of the loop. –  Sep 01 '19 at 21:59
  • Thanks for the answer, @ChrisG ! However, can I not just do `node.textContent+= "Something"` instead of appending? I would think it is better than to create a new node and it even requires less code. – Daniel A. Sep 01 '19 at 21:59
  • Yes, that will also work. –  Sep 01 '19 at 22:01
  • @ChrisG (Regarding the body.append...) Very true, thanks for the tip! – Daniel A. Sep 01 '19 at 22:01
  • 1
    There are also advantages to working with an element independent of the DOM. Efficiencies are gained when manipulating the element (e.g, concatenating strings). Such reasons have given way to the concept of _virtual DOM_ and detaching elements, so direct DOM manipulation isn't performed until all actions/activity is finished, thus reducing the overhead of acting on the DOM. – vol7ron Sep 01 '19 at 22:03
  • Thanks all for the answers! @vol7ron I did not find previously the answered question; thanks for directing me to it, though it does not fully explain the difference, as my method in the comment above does virtually the same. However, I found ONE difference: the `textContent` property edits the object (located in the document object) directly. The `appendChild()` method actually adds it with a line break (codewise, **not** an actual `
    `) and separates the text with `""`, which potentially means you could insert a `` (or other tags) in the newly added text.
    – Daniel A. Sep 01 '19 at 22:48
  • @vol7ron Also, your comment actually answered more than the answered question you directed to :) – Daniel A. Sep 01 '19 at 22:48