Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:06:05+00:00 2026-06-17T23:06:05+00:00

Working with an unordered (or ordered) list in a contenteditable gives me a headache.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:06:06+00:00Added an answer on June 17, 2026 at 11:06 pm

    Similar to your attempt, we modify the contenteditable when the user presses Enter: Demo

    if (window.getSelection) { // w3c
        $('div').keypress(function (e) {
            var sel, node, children, br, range;
            if (e.which == 13) {
                sel = window.getSelection();
                node = $(sel.anchorNode);
                children = $(sel.anchorNode.childNodes);
    
                // if nothing is selected and the caret is in an empty <li>
                // (the browser seems to insert a <br> before we get called)
                if (sel.isCollapsed && node.is('li') && (!children.length ||
                        (children.length == 1 && children.first().is('br')))) {
                    e.preventDefault();
    
                    // if the empty <li> is in the middle of the list,
                    // move the following <li>'s to a new list
                    if (!node.is(':last-child')) {
                        node.parent().clone(false)
                            .empty()
                            .insertAfter(node.parent())
                            .append(node.nextAll());
                    }
    
                    // insert <br> after list
                    br = $('<br>').insertAfter(node.parent());
    
                    // move caret to after <br>
                    range = document.createRange();
                    range.setStartAfter(br.get(0));
                    range.setEndAfter(br.get(0));
                    sel.removeAllRanges();
                    sel.addRange(range);
    
                    // remove <li>
                    node.remove();
                }
            }
        });
    
    } else if (document.selection) { // internet explorer
        $('div').keypress(function (e) {
            var range, node, children;
            if (e.which == 13) {
                range = document.selection.createRange();
                node = $(range.parentElement());
                children = $(range.parentElement().childNodes);
    
                // if nothing is selected and the caret is in an empty <li>
                // (the browser seems to insert a <br> before we get called)
                if (!range.htmlText.length && node.is('li') && (!children.length ||
                        (children.length == 1 && children.first().is('br')))) {
                    e.preventDefault();
    
                    // if the empty <li> is in the middle of the list,
                    // move the following <li>'s to a new list
                    if (!node.is(':last-child')) {
                        node.parent().clone(false)
                            .empty()
                            .insertAfter(node.parent())
                            .append(node.nextAll());
                    }
    
                    // insert <br> after list
                    br = $('<br>').insertAfter(node.parent());
    
                    // move caret to after <br>
                    range = document.body.createTextRange();
                    range.moveToElementText(br.get(0));
                    range.collapse(false);
                    range.select();
    
                    // remove <li>
                    node.remove();
                }
            }
        });
    }
    

    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>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a front-end web app where a nested unordered list would
I'm working on a two level unordered list menu, and so far was able
I'm working in JavaScript. I'd like to store a list of unique , unordered
I am working with scriptaculous' autocomplete. I am returning an unordered list but each
I want to generate a unordered list which contains tag for showing images in
I'm working with an unordered list of links that has two levels: <ul class=level-1>
I was working on a slider using an unordered list. Standard structure, example (not
On the site I'm currently working on there is a nested unordered list in
I am working on an un-ordered list. I have searched all over the internet
I have an unknown number of unordered lists and I want to display them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.