I am creating a simplified and easy-to-use text editor in Javascript, mostly a WYSIWYG editor. I will use the contenteditable
attribute for the main shell ( .wrapper
). When you press enter inside .wrapper
, a new new <p>
element with a unique id
added to the wrapper.
I need a way to select the child .wrapper
element that is currently selected (i.e. focused or has a cursor / text marker inside it).
I searched for days without any results, and I tried using document.elementFromPoint()
, but without any correct results.
When using $(":focus")
I get the whole .wrapper
, not the specific child.
Edit:
HTML structure example:
<div class="container t-wrapper" contenteditable> </div>
Javascript Code Example:
$(document).ready(function() { $currentElement = $("[contenteditable]"); $(".t-wrapper").each(function() { var id = generateIdentifier(6);
Edit 2:
I managed to get it to work with the mouseup
event, since you are actually clicking on something. But I need this to work when moving the caret using the keyboard . In addition, I need to somehow get the position of the caret in pixels, and then use document.elementFromPoint()
to get a specific element.
source share