Skip to content

Instantly share code, notes, and snippets.

@tcr
Created October 26, 2011 23:02
Show Gist options
  • Save tcr/1318279 to your computer and use it in GitHub Desktop.
Save tcr/1318279 to your computer and use it in GitHub Desktop.
How do different browsers handle empty paragraphs/blocks using contentEditable?
<!-- most modern browsers -->
<p><br></p>
<!-- ie9 -->
<p></p>
<!-- ie7-8 -->
<script>
document.body.appendChild(document.createElement('p'))
</script>

Empty paragraphs would be those which have no editable content, but are still selectable via cursor and let content be added to them.

From my tests:

  1. 'Modern' browsers (Chrome/Firefox) -- Adds a
    as the last element. This
    can't be edited/selected, but it keeps the tag artificially "open" whenever there's no other content in it. Can be written using HTML.

  2. IE9 -- Empty paragraphs can be fully empty and still editable.

  3. IE7--8 -- Empty paragraphs are fully empty and still editable. However, innerHTML reads as though there was an   in the paragraph (there's not). Also, manually writing <p>&nbsp;</p> in HTML actually adds an editable space, but <p></p> isn't editable! Turns out this works only if you add an empty paragraph via DOM methods (with no inner nbsp)

["innerhtml","contenteditable","space","html","dom","ie","browers","compatibility"]
@shenyan008
Copy link

maybe a bug in webkit since 2005
https://bugs.webkit.org/show_bug.cgi?id=4003

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment