Forum Moderators: open
<DIV ID="notes" NAME="notes" onPaste="clean_pasted_content('notes');">
function clean_pasted_content(area_div) {
var div_to_clean = document.getElementById(area_div);
var do_again = 1;
while (do_again) {
do_again = 0;
if (div_to_clean.hasChildNodes()) {
for (var x = 0; x < div_to_clean.childNodes.length; x++) {
var child_node = div_to_clean.childNodes[x];
if (child_node.nodeType == 3) { continue; }
if (child_node.nodeType == 1) {
if (child_node.nodeName == 'BR') { continue; }
do_again = 1; // Not a BR, do stuff and re-scan
no_elements(div_to_clean);
}
}
}
}
div_to_clean.normalize();
}
function no_elements(parent) {
var parent_type = parent.nodeType;
if (parent.hasChildNodes()) {
for (var x = 0; x < parent.childNodes.length; x++) { // scan all children nodes
var child_node = parent.childNodes[x];
if (child_node.nodeType == 3) { // move text nodes up if not already in the DIV
if ((parent_type == 1) && (parent.nodeName != 'DIV')) {
move_element_up_dom(parent, child_node);
continue;
}
}
if (child_node.nodeType == 1) {
if ((child_node.nodeName == 'BR') && (parent.nodeName != 'DIV')) {
move_element_up_dom(parent, child_node);
} else {
no_elements(child_node);
}
}
}
} else { // no children...
if (parent.nodeType == 1) {
if ((parent.nodeName != 'BR') && (parent.nodeName != 'DIV')) {
parent.parentNode.removeChild(parent);
}
}
}
}
function move_element_up_dom(parent, child) {
var clone = child.cloneNode(true);
parent.parentNode.insertBefore(clone, parent);
if (! child.hasChildNodes()) { parent.removeChild(child); }
}