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 8825501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:53:38+00:00 2026-06-14T06:53:38+00:00

I’m trying to allow users to edit a list (UL). In my attempts, it

  • 0

I’m trying to allow users to edit a list (UL). In my attempts, it appears that contenteditable doesn’t do anything special (like enforcing behind-the-scenes markup) — it just gives the user a window into the innerHTML.

This is causing issues in that if there is not already a LI, and the user adds something, it doesn’t get LI-ized. Similarly, if there are list items, but the user deletes them, then the LI gets deleted, and any new text is added without LI’s. See http://jsfiddle.net/JTWSC/ . I’ve also found that it’s sometimes possible for the cursor to “get outside” of an LI that does exist, but I can’t reproduce consistently.

I have to include code, so this is what the “result” looks like:

<ul>whatever the user typed in</ul>

How do I fix this? I started down the path of a $(‘ul’).keyup() handler that checks the html and wraps as necessary, but I was running into a handful of gotchas, like timing, losing focus on the element, having to refocus in the right place, etc. I’m sure it’s possible if I work at it, but I’m hoping for an easier solution.

  • 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-14T06:53:40+00:00Added an answer on June 14, 2026 at 6:53 am

    I built the following keyup/down handler in order to make my contenteditable <UL>s idiot proof.*

    It does two things:

    1. Adds <LI>s to the <UL> when the <UL> is empty. I use some code I found on SO (from Tim Down) to place the caret in the expected place
    2. Cleans up all non-LI / non-BR tags. This is basically a quick-and-dirty paste-cleaner.

    This is pushing my comfort-level on jquery and DOM manipulation, so there are probably a few things I could do better, but it works pretty well as-is.

    //keyup prevented the user from deleting the bullet (by adding one back right after delete), but didn't add in li's on empty ul's, thus keydown added to check
    $('ul').on('keyup keydown', function() {
      var $this = $(this);
        if (! $this.html()) {
            var $li = $('&lt;li&gt;&lt;/li&gt;');
            var sel = window.getSelection();
           var range = sel.getRangeAt(0);
            range.collapse(false);
            range.insertNode($li.get(0));
            range = range.cloneRange();
            range.selectNodeContents($li.get(0));
            range.collapse(false);
            sel.removeAllRanges();
            sel.addRange(range);
    
        } else {
            //are there any tags that AREN'T LIs?
            //this should only occur on a paste
            var $nonLI = $this.find(':not(li, br)');
    
            if ($nonLI.length) {
                $this.contents().replaceWith(function() {
        //we create a fake div, add the text, then get the html in order to strip out html code. we then clean up a bit by replacing nbsp's with real spaces
    return '&lt;li&gt;' + $('&lt;div /&gt;').text($(this).text()).html().replace(/&nbsp;/g, ' ') + '</li>';
                });
                //we could make this better by putting the caret at the end of the last LI, or something similar
            }                   
        }
    });
    

    jsfiddle at http://jsfiddle.net/aVuEk/5/

    *I respectfully disagree with Diodeus that training is the best / easiest solution in all cases. In my situation, I have several contenteditable <UL>s on a page that are very in-line WYSIWYG (ie, not a lot of room for tinymce-style chrome) and used by casual, first-time, non-advanced users.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.