getElementById("frm").onsubmit = function(){
var passcode = document.getElementById("passcode");
if(!passcode.regexp.test(passcode.value))
{
var el = document.createElement("div");
el.className = 'error';
el.innerHTML = 'Not a valid passcode';
document.getElementsByTagName("body")[0].appendChild(el);
return false;
}
}
When it comes to rapid development, I highly recommend that you avoid applying styles
directly to an element unless the value must be calculated at runtime (for example, animation).
Just as CSS establishes a separation of content and presentation with HTML, doing it in
this way helps maintain a separation between presentation and behavior with JavaScript.
Inserting Content into the DOM
In the preceding example, a few more features of the DOM were used. The first is the
createElement() method, which creates a new HTML element but sits in limbo until you insert
it into the document. There are three DOM methods to add new content into the document:
??? appendChild(): Adds the element as the last child of a parent element.
??? insertBefore(): Adds a new element before an element that you specify.
??? replaceChild(): Replaces an existing element in the DOM with the element that you
want to add. You can also use this to replace one element with another element already
on the page.
In addition to these three methods, there is a fourth (currently nonstandard) way of
adding content into the document: the innerHTML property.
Pages:
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82