I'm trying to write a wysiwyg editor using a contentEditable div, and am having trouble in Firefox when dealing with bold (and italic).
When all text in the div is selected, execCommand('bold') works in the div, but when I try to grab the HTML of that content, the HTML does not reveal any formatting.
To see what I mean, please try running the following HTML code in Firefox 5:
<script type="text/javascript">
window.onload = function () {
var output = document.getElementById('output');
var input = document.getElementById('input');
}
</script>
<body>
<button onClick="execCommand('bold', false, null);output.value=input.innerHTML;">Bold</button>
<div style="width: 300px; border: 1px solid #000000;">
<div id="input" contenteditable="true">Some editable text</div>
</div>
<textarea id="output"></textarea>
</body>
</html>
Please try the following:
- Select the word "Some" only. Click the Bold button. This will make the text bold, as expected. The HTML output uses
<span style="font-weight:bold;">
to bold the word, which is somewhat unfortunate (it's<b>
in Safari, Chrome), but nonetheless does the job. - Select the entire phrase "Some editable text" (either manually or using CTRL-A). Click the Bold button. While the contentEditable text is bold, as expected, the HTML output does not have the bold formatting applied.
Any ideas on what could be causing these problems and how to work around them would be greatly appreciated!
<div contenteditable="true" id="input" style="font-weight: bold;">Some editable text</div>
. Since the wholeinput
has hadbold
applied to it, it applies it as a style to theinput
instead of creating aspan
. – Intensifier