Working with an unordered (or ordered) list in a contenteditable gives me a headache.
Whenever I want to end editing the list by pressing ENTER twice the browser will close the <ul /> but inserts a <p /> (Firefox) or a <div /> (Chrome) tag that contains a <br />.
Example here
My goal is to avoid that superfluous <p /> or <div /> and instead just close the <ul />.
I have tried to modify Tim Down’s solution which will prevent the browser to insert <p /> or <div /> when pressing ENTER and instead inserts a clean <br /> tag.
Example here
Unfortunately though, when using that solution the <ul /> is never closed by the browser since only <br /> tags are inserted inside the <li /> item.
So my question is:
How can I actively close the <ul /> by inserting a node or pasting html when pressing enter on the last empty <li />?
Update: In case the question is stll unclear: I am looking for a way to close the <ul /> without inserting <p /> or <div /> tags but just by inserting a good old plain <br /> instead
Similar to your attempt, we modify the contenteditable when the user presses Enter: Demo
Note that this doesn’t handle the case where the user has selected something before pressing Enter. If you want to handle this case, you’ll need to figure out if the user has selected the entire contents of the
<li>(this doesn’t seem like a trivial task), and if so, delete the contents and treat it the same as if the user pressed Enter in an empty<li>.