D
dennis.sprengers
I am trying to write an editor object, which adds some functionality
and a toolbar to every textarea with a "form-textarea" class. Both FF
and IE generate an error in line 20
(container.appendChild(this.toolbar)
saying "Node cannot be inserted
at the specified point in the hierarchy" code: "3"
I have two questions:
- who would help me with this error
- please provide other feedback about my code, since I want to write a
solid framework
I've pasted all code (including general library functions), to provide
a complete overview.
Any help is greatly appreciated,
Dennis
/********************************** editor code
*******************************/
var Editor = {
version : "1.0 beta",
init : function() {
var textareaNodes = document.getElementsByTagName('textarea');
var thisTextarea, i = 0;
while ((thisTextarea = textareaNodes[i++])) {
if (hasClass(thisTextarea, 'form-textarea')) {
new this.textEditor(thisTextarea);
}
}
},
textEditor : function(textarea) {
this.textarea = textarea;
this.value = textarea.value.replace(/\s+$/g, "");
this.textarea.className = 'editor-textarea';
this.textarea.rows = this.value.split("\n").length;
var container = createNode('div', {
'class' : 'texteditor ' + this.textarea.id}
);
this.toolbar = Editor.textToolbar;
container.appendChild(this.toolbar);
container.appendChild(this.textarea);
this.textarea.parentNode.insertBefore(container, this.textarea);
Editor.detectKey(this.textarea);
this.textarea.focus();
},
detectKey : function(textarea) {
var theHandler = this.handleKey;
if (document.all) textarea.onkeydown = function(e) {
theHandler.call(self, e, textarea);
}
else textarea.onkeypress = function(e) {
theHandler.call(self, e, textarea);
}
},
handleKey : function(e, textarea) {
if (!e || e == null) var e = window.event;
if (e.keyCode) key = e.keyCode;
else if (e.which) key = e.which - 32;
Editor.growTextarea(textarea);
},
growTextarea : function(textarea) {
var value = textarea.value, newlines = value.split("\n"), i = 0,
rows = 1;
while (newline = newlines[i++]) {
rows += Math.ceil(newline.length / textarea.cols);
}
textarea.rows = rows;
},
textToolbar : function() {
str = createNode('input', {'type' : 'checkbox'});
return str;
}
}
addLoadEvent(function () {
Editor.init();
});
/******************************* library functions
****************************/
function addLoadEvent(func) {
var oldOnload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
}
else {
window.onload = function() {
oldOnload();
func();
}
}
}
function hasClass(el, className) {
return (
el.className &&
el.className.match(new RegExp("\\b" + className + "\\b"))
) ? true : false;
}
function createNode(type, options) {
var el = document.createElement(type);
for (var key in options) {
el.setAttribute(key, options[key]);
}
return el;
}
and a toolbar to every textarea with a "form-textarea" class. Both FF
and IE generate an error in line 20
(container.appendChild(this.toolbar)
at the specified point in the hierarchy" code: "3"
I have two questions:
- who would help me with this error
- please provide other feedback about my code, since I want to write a
solid framework
I've pasted all code (including general library functions), to provide
a complete overview.
Any help is greatly appreciated,
Dennis
/********************************** editor code
*******************************/
var Editor = {
version : "1.0 beta",
init : function() {
var textareaNodes = document.getElementsByTagName('textarea');
var thisTextarea, i = 0;
while ((thisTextarea = textareaNodes[i++])) {
if (hasClass(thisTextarea, 'form-textarea')) {
new this.textEditor(thisTextarea);
}
}
},
textEditor : function(textarea) {
this.textarea = textarea;
this.value = textarea.value.replace(/\s+$/g, "");
this.textarea.className = 'editor-textarea';
this.textarea.rows = this.value.split("\n").length;
var container = createNode('div', {
'class' : 'texteditor ' + this.textarea.id}
);
this.toolbar = Editor.textToolbar;
container.appendChild(this.toolbar);
container.appendChild(this.textarea);
this.textarea.parentNode.insertBefore(container, this.textarea);
Editor.detectKey(this.textarea);
this.textarea.focus();
},
detectKey : function(textarea) {
var theHandler = this.handleKey;
if (document.all) textarea.onkeydown = function(e) {
theHandler.call(self, e, textarea);
}
else textarea.onkeypress = function(e) {
theHandler.call(self, e, textarea);
}
},
handleKey : function(e, textarea) {
if (!e || e == null) var e = window.event;
if (e.keyCode) key = e.keyCode;
else if (e.which) key = e.which - 32;
Editor.growTextarea(textarea);
},
growTextarea : function(textarea) {
var value = textarea.value, newlines = value.split("\n"), i = 0,
rows = 1;
while (newline = newlines[i++]) {
rows += Math.ceil(newline.length / textarea.cols);
}
textarea.rows = rows;
},
textToolbar : function() {
str = createNode('input', {'type' : 'checkbox'});
return str;
}
}
addLoadEvent(function () {
Editor.init();
});
/******************************* library functions
****************************/
function addLoadEvent(func) {
var oldOnload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
}
else {
window.onload = function() {
oldOnload();
func();
}
}
}
function hasClass(el, className) {
return (
el.className &&
el.className.match(new RegExp("\\b" + className + "\\b"))
) ? true : false;
}
function createNode(type, options) {
var el = document.createElement(type);
for (var key in options) {
el.setAttribute(key, options[key]);
}
return el;
}