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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:51:38+00:00 2026-05-20T22:51:38+00:00

I am trying to use event delegation inside a containing div tag set to

  • 0

I am trying to use event delegation inside a containing div tag set to contenteditable. For focus and keypress events the event is captured by the element with the contenteditable set to true. I want the element that the event happened on to be returned. Is there anyway for the event to register on the element you are actually working on without attaching events to each one, or setting every element to contenteditable=”true”?
If you click on any of the element d1 it returns element top not d1.

<div id="top" contenteditable="true">
<div id="d1">Edit</div>
<div id="d2">Edit</div>
<div id="d3">Edit</div>
</div>

window.onload=function(){
document.getElementById('top').addEventListener('focus',function(e){
  console.log(e.target);
},true);
}
  • 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-20T22:51:38+00:00Added an answer on May 20, 2026 at 10:51 pm

    I don’t know how to do this in pure js, but I know it can be done in jQuery using .closest()

    Even if you don’t want to use jQuery, you may search for how it works. Good luck!

    Edit:

    I’ve just tested the code here in Opera… Don’t know if it’s a bug, tough… But using to ‘click’ instead of ‘focus’ make it correctly get the id of the inner div

    Take a look:

    <script type="text/javascript">
    window.onload = function(){
      document.getElementById('top').addEventListener('click',function(e){
        alert(e.srcElement.id); //this alert d1 to d3
      },true);
    document.getElementById('top').addEventListener('focus',function(e){
        alert(e.srcElement.id); //this alert top
      },true);
    }
    </script>
    
    <div id="top" contenteditable="true">
          <div id="d1">Edit</div>
          <div id="d2">Edit</div>
          <div id="d3">Edit</div>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use focus event on list element. it's working with hover,
I'm trying to use jQuery's $(this) inside of Fancybox's onComplete event, but I'm running
I am trying to use the Event Log to write some debug information and
I'm trying to use an event listener for the first time. All samples I've
I'm trying to use an event dispatcher to allow a model to notify subscribed
I'm trying to use a timer to schedule a recurring event in an application.
I'm trying to use e4x to retrieve xml from the result event thrown when
I am trying to add click event to one of the widgets that use
I'm trying to write a callback event system in DirectX9. I'm attempting to use
I was trying use a set of filter functions to run the appropriate routine,

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.