Why does document.createTextNode() not allow setAttribute()?

javascript

Solution

you cant set/get any of attributes/elements of textNode

are there any workarounds?

it's easy to say creating element inside such as `span` and set your text

var genericElementNode = document.createElement('p');
genericElementNode.setAttribute('id', 'sampleId1');
// The above will run fine


var textNode = document.createElement("span");
textNode.innerText = "Hello World";
textNode.setAttribute('id', 'sampleId2');

Problem

So while working with the dom, I came across the situation where I assumed that the object resulting from `document.createTextNode()` would be treated in a similar way to an object resulting from `document.createElement();`, in that I would be able to call `setAttribute()` on it. Example: ``` var genericElementNode = document.createElement('p'); genericElementNode.setAttribute('id', 'sampleId1'); // The above will run fine var textNode = document.createTextNode("Hello World"); textNode.setAttribute('id', 'sampleId2'); //The above will result in an error: //Uncaught TypeError: textNode.setAttribute is not a function ``` Why is this the case? And are there any workarounds?

Original source