Java Reference
In-Depth Information
the newPara node. This means that it is newPara that calls the method with text as an
Now we have a paragraph element that contains the text that we want. So the process to
follow each time you want to create a new element with content is this:
1. create the element node
2. create the text node
3. append the text node to the element node
This can be made simpler by using the textContent property that every element object
has. This will add a text node to an element without the need to append it:
var newPara = document.createElement('p');
newPara.textContent = 'Transition 1';
While this has cut the number of steps from three down to two, it can still become repetit-
ive, so it's useful to write a function to make this easier. This is what we'll do next.
Putting It All Together in a Function
When we created our new paragraph element, all we specified were the type of tag we
wanted to use and the text inside it. These will form the parameters of our function. The
function will then perform the two steps we used to create the new element and then return
that element:
function createElement (tag,text) {
el = document.createElement(tag);
el.textContent = text;
return el
Let's try it out by creating another new paragraph element:
Search WWH ::

Custom Search