0

I keep learning very simple things about functions, returns, id and everything, so I ran into another problem that looks simple, but I cannot understand why it happens. Check this code:

function test() {
    var text = document.createTextNode("Hello");
    text.id = "t";
    }


var whatIjustwrote = window.document.getElementById("t");
alert(whatIjustwrote);​

Does the getElementById has restrictions to look only for global items? What would be the way to make that alert output the text node inside the function?

Thank you for any comment. The last few days asking things here I have been learning quite a lot!

JSFiddle

telex-wap
  • 832
  • 1
  • 9
  • 30

4 Answers4

5

Firstly, getElementById will only return an element, and you're creating a text node.

Secondly, it will only return an element that has been added to the DOM. The node you create doesn't get added to the DOM, so it wouldn't be found even if it could be.

Finally, you don't actually call the test function, so the text node isn't even created in memory.

Here's an updated fiddle that demonstrates getElementById actually working:

function test() {
    var text = document.createElement("span"); //Create an element
    text.innerHTML = "Hello";
    text.id = "t";
    document.body.appendChild(text); //Add it to the DOM
}

test(); //Invoke the function (so the element actually gets created)
var yourElement = document.getElementById("t"); //Get reference to element
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • Thank you, very educative answer that explained what I didn't understand. – telex-wap Nov 07 '12 at 12:16
  • @James: You right should have showed the newbie [why not to user innerHTML](http://stackoverflow.com/a/2305677/1048572) :-) – Bergi Nov 07 '12 at 12:31
2

getElementById does only search for element nodes. You did create a text node, which has neither attributes nor an id - you just added a custom property to the JS object. Also, you did not append your node to the document, so it couldn't have been found in the DOM tree.

You might want to read an introduction to the DOM (at MDN), the introduction at quirksmode.org or even the W3 standard itself (especially the introduction section)

function test() {
    var elem = document.createElement("span"); // Create an element
    var text = document.createTextNode("Hello"); // Create a textnode
    elem.appendChild(text); // add text to the element
    elem.id = "t"; // assign id to the element
    document.body.appendChild(elem); // Add it to the DOM
}
test();

var yourElement = document.getElementById("t"); // Get the element from the DOM
alert(yourElement.textContent); // alerts "Hello"
// you also could have alerted yourElement.firstChild.data - the content of the 
// textnode, but only if you had known that yourelement really has a firstchild

(Demo at jsfiddle.net)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I'll be sure to read that! The DOM is one of the parts that confuses me the most in JS (I am new to programming in general), but I realize about the importance of it. – telex-wap Nov 07 '12 at 12:17
1

Does the getElementById has restrictions to look only for global items?

The answer is no. First you have to define global items anyways. Anything that is attached to the DOM is in fact global, and in terms of global javascript objects there is only one, window, in the case of a browser. You are creating a function but you're never executing it.

In addition the text node cannot actually have an id or any other attribute. You need an element for this, so even if you execute the function you would still get null. Also creating a node does not attach is to the DOM, so you won't be able to access it even if this isn't a text node.

I have updated your fiddle.

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
1

A couple of points that come to mind..

1) You cant give a textNode an id attribute (you're actually giving it a new member variable named id) 2) To find an element it must exist in the document's DOM

Do this instead:

var mSpan = document.createElement('span');
mSpan.id = 't';
mSpan.appendChild( document.createTextNode('Hello') );
document.body.appendChild(mSpan);

var whatIjustwrote = window.document.getElementById("t");
alert(whatIjustwrote.innerText);
enhzflep
  • 12,927
  • 2
  • 32
  • 51
  • Perfect, you're welcome. I find the Chrome dev-tools console (Hit Ctrl-Shift-I) can be really helpful. Hit F12 in IE for the same thing, Ctrl-Shift-I in opera - not sure about FF or Safari. The console makes things much, much easier. There's oddles of vids out there to give you a tour of it too. Enjoy your time in javascript land! :) – enhzflep Nov 07 '12 at 12:22