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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:41:59+00:00 2026-05-20T04:41:59+00:00

I have a CONTENTEDITABLE div and inside that div I have a CONTENTEDITABLE span,

  • 0

I have a CONTENTEDITABLE div and inside that div I have a CONTENTEDITABLE span, what I want to do is being able to handle the onkeypress event on the inner SPAN.

So, the javascript code would be:

$(function()
{
    $('#someid').keypress(function(event){alert('test');});
});

And the HTML content would be:

<div id="mydiv" contenteditable="true">
editable follows:<span id="someid" contenteditable="true">Some TEXT</span>
</div>

If you test it on a browser you’ll see you won’t see the ‘test’ dialog when you press a key over Some TEXT, I know the problem is that the event is being triggered in the parent div, so the SPAN doesn’t get the event, also because it doesn’t have the focus. So I’d like your help to find a solution for this.

  • 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-20T04:41:59+00:00Added an answer on May 20, 2026 at 4:41 am

    The exact code you posted in your question seems to work just fine at http://jsfiddle.net/gaby/TwgkC/3/

    Tested and working with FF, Opera, Chrome, Safari, IE8 ..

    only change is the removal of the comment which in its current form creates a syntax error.

    The #someid need to have focus in order for the keypress to work.
    If you want your code to give focus to the element right after creating it, use the .focus() method.

    function AppendSpan()
    {
        $('#mydiv').append('<span id="someid" contenteditable="true">Some TExt</span>');
        //Then I want to handle the keypress event on the inserted span
        $('#someid').keypress(function(event){
              //do something here
              alert(this.id);
        }).focus();// bring focus to the element once you append it..
    }
    

    Update

    Two ways to handle this (the fact that there are nested contenteditable elements), not sure if any is acceptable for your case but here they are..

    1. wrap the new contenteditable span in another one, which is set to have contenteditable="false"
      (demo: http://jsfiddle.net/gaby/TwgkC/10/)
    2. make #mydiv to not be contenteditable once you add the span..
      (demo: http://jsfiddle.net/gaby/TwgkC/11/)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a contenteditable div that I want users to be able to select
have this html: <div id=editable contentEditable=true > <span contentEditable=false >Text to delete</span> </div> need
I have a <div> that has contentEditable=true . When I copy-paste content within the
I have a contentEditable div where I want to insert HTML tags (a simple
I have a div element with contenteditable set to true, and I want to
Let's say I have a contentEditable div , to the user can edit and
I have a contenteditable div where I need to insert text at the caret
I have some elements that need to have the text inside editable, for this
I am using contentEditable div tags on my website and I have noticed a
I have a contentEditable div with following text for example: <div contentEditable='true'> This document

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.