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

  • Home
  • SEARCH
  • 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 6176801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:09:53+00:00 2026-05-24T00:09:53+00:00

I have a content editable div. Within this I am inserting a <pre>some code</pre>

  • 0

I have a content editable div. Within this I am inserting a <pre>some code</pre> tag. This tag is designed to allow the user to enter formatted code, similar to SO.

The problem I am having is that FF is inserting <br> tags in place of \r\n which in normal Html would be correct but in a pre I actually want the \r\n.

I have tried attaching a delegate to the parent contenteditable. But this does not fire for nested dom elements. So

$('#contenteditablediv').delegate('pre', 'keyup', function() { ... });

Does not fire. I’ve also tried handling the normal keyup on the parent div and replacing all brs in pre tags with \r\n. But this messes up the caret and is clunky.

Is there a preferred way of doing this?

I may have to resort to stripping them server side but I’d rather not.

Many thanks

  • 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-05-24T00:09:54+00:00Added an answer on May 24, 2026 at 12:09 am

    I have actually found a solution. It isn’t perfect and I am considering having a go at integrating codemirror as suggested by Mrchief. The only problem is that my current solution alows me to edit the markup that will be saved in the back-end and displayed later. It is truly WYSIWYG. I should have mentioned that my editor is part of a greater contenteditable article, in which I want to add some code fragments.

    Using an editor like codemirror would be awesome but Id have to strip it out before saving and my code blocks would have to be displayed as textarea (not as RSS friendly as pre).

    Anyways for anyone that’s interested here is my solution/hack.


    You can delegate events from nested <pre> tags etc in contenteditables as long as they are wrapped in an element with contenteditable="false". So instead of inserting:

    <pre>some code</pre

    I inserted:

    <code contenteditable="false"><pre>code snippet here</pre></code>

    I then added a delegate like so.

    var getFirstRange = function() {
        var sel = rangy.getSelection();
        return sel.rangeCount ? sel.getRangeAt(0) : null;
    }
    
    contenteditablediv
        .delegate('pre', 'keydown', function(event) { 
            switch(event.keyCode) {
                case 13:
                    var range = getFirstRange(), 
                        added = false,
                        newline = document.createTextNode('\r\n');
    
                    if (range) {
                        range.insertNode(newline);              
                        range.setEndAfter(newline);
                        range.setStartAfter(newline);
                        var sel = rangy.getSelection();
                        sel.setSingleRange(range)
                        added = true;
                    } 
    
                    if (added) {
                        event.preventDefault();
                    }
                    break;
                case 9:
                    // insert a tab
                    var range = getFirstRange(),
                        tab = document.createTextNode('\t');
                    if (range) {
                        range.insertNode(tab);
                        var sel = rangy.getSelection();
                        range.setEndAfter(tab);
                        range.setStartAfter(tab);
                        sel.setSingleRange(range)
                    } 
                    return false;   
            }
        }).delegate('pre', 'click', function() { 
            $(this).attr('contenteditable', true);
        }).delegate('pre', 'blur', function() {
            $(this).removeAttr('contenteditable');
        });
    

    This event fires. I have added a click/blur delete to toggle contenteditable on the inner pre tag so this doesn’t get saved down after but that could be done server-side. This allows me to insert \t characters on tab key and \r\n in place of br.

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

Sidebar

Related Questions

I have contenteditable div with text on it. In firefox, there is some kind
I have a contenteditable div on my site which works great. On Chrome i
Hello i need to colorize some text in the contentEditable div. Let's say i
Currently I am making an editor using contenteditable. I have the following: <div contenteditable=true>
I have a three column layout with a container three-col In the right column
i have the following function in jQuery and javascript that creates a form element:
I'm using this Plugin to count chars on inputs and textareas: http://cssglobe.com/post/7161/jquery-plugin-simplest-twitterlike-dynamic-character-count-for-textareas I would
As far as I know this isn't possible. I would like to draw an
I am developing a WYSIWYG editor using the iframe technique. I need to know
I'm trying to write a lightweight html/javascript widget which lets me colour each character

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.