You shouldn't get the nodeValue of the node without checking its type. If prevSibling is an element, you get a javascript error (because nodeValue will be null and you are invoking .trim() on it).