View Full Version : innerHTML changes div tag case and removes quotes

12-24-2004, 05:18 PM
I have a WYSIWYG editor. I am getting the contents of the editor via the Javascript command:

str = Composition.document.body.innerHTML;

The problem I'm having is innerHTML converts the case of the DIV tag entered from lowercase to uppercase and strips out the quotes.

<div class="test">


<DIV class=test>

This doesn't affect the display of the HTML, but customers are concerned that their HTML is getting changed. I've tried to use the following to get the DIV tags back to the original case and with quotes:

myre = new RegExp("DIV class=(.*)>","gi");
mynewstr = str.replace(myre,"div class=\"$1\"");

The above does not work. Anytime I try and escape the quotes, it fails. The below fails as well.

mynewstr = str.replace(myre,'div class=\"$1\"');

This works, but obviously doesn't have the quotes around the value:

mynewstr = str.replace(myre,"div class=$1");

Any help would be appreciated.



Willy Duitt
12-27-2004, 04:50 AM
Try using the DOM: createElement() rather than the deprecated innerHTML....


12-27-2004, 07:25 AM
innerHTML does not change anything... The browser reformats code before parsing because it is much simpler/faster when the order of everything is absolute.

To undo this, you'll have to re-reformat the code, by enumerating all properties, sorting, changing case, etc., or just use the browser's download behavior to view the source file (if code is not dynamically being altered).