HTML and CSS Reference
In-Depth Information
EXPLANATION
4
A reference to a new
br
element is created.
5
A new text node is created to be placed in each paragraph.
6
A unique
id
attribute is set for each paragraph.
7
The
getElementById()
method returns a reference to the paragraph identified as
“aPara1”. The alert method displays the value of the reference.
Figure 15.17
Creating new elements and attributes with the DOM.
The next example, Example 15.10, is a program demonstrating how to use the DOM
methods and properties we have covered thus far. The idea is to dynamically add new
text entries, called blogs, into a Web page. The user will be presented with an area in
which to write his or her blog. After the user clicks the “Add a blog” button, JavaScript
will create a blog object and use DOM methods to define the structure of the new entry
in the document.
1
Each blog entry will be appended to the previous one with the blog-
ger's name, the date the blog was posted, and the blog message.
1. To save the blogs in a database or file, you will need a server-side program such as PHP or ASP.NET.