2

Imagine I have some element

<p>Hello</p>

user click between e and l letter (put caret in this place) , on this click I want insert some element between those letters . It will be

<p>He<b>Here</b>llo</p>

is there any way to make it ?

Code example:

  const selection = document.getSelection();
  const parent = selection.baseNode.parentNode;
  const b = document.createElement('b')
  b.innerHTML = 'Here'

  parent.innerHTML = 'he'+ b + 'llo'

It will not work as you understand , I need solution for this case. Caret position I can determine

selection.focusOffset

and this mean that I have to get selection text , and insert on caret position <b>Here</b> element. I've also tried to find method to convert element to string , but haven't foudn . I mean document.createElement(tagName) make it as string , then I can insert it in text

3
  • Yes, but yoi need to post a minimal reproducible example. I don't want to write all of the JavaScript, too much work, so post some broken JS and I'll try to fix it. Commented Jan 18, 2022 at 20:31
  • Determining where to put the caret on clicking inside a text node is not an easy problem to solve -- there is a non-standard method for it but it's not universally available. (The rest of the question is trivial, just insert whatever you want once you've figured out where exactly to insert it.) Commented Jan 18, 2022 at 20:38
  • Updated little bit , if it will be helpfull Commented Jan 18, 2022 at 20:43

3 Answers 3

1

You can use the insertNode method of a Range in the Selection to do it.

Here's a basic example:

const p = document.querySelector('p')
p.addEventListener('click', () => {
  const selection = document.getSelection();
  if(!selection.isCollapsed || !selection.containsNode(p, true))
    return
  const b = document.createElement('b')
  b.innerText = 'here'
  const range = selection.getRangeAt(0)
  range.insertNode(b)
  range.collapse() //unselect inserted text
})
<p>Some text</p>

Sign up to request clarification or add additional context in comments.

Comments

0
      const selection = document.getSelection();
      const text = selection.anchorNode.data;
      const parent = selection.baseNode.parentNode;
      const position = selection.focusOffset;

          const html = [
            text.slice(0, position),
            createdElementSpan.outerHTML,
            text.slice(position),
          ].join('');

          parent.innerHTML = html;

Seems like it works, outerHTML property return string . But also have to find out how to put cursor in correct position

Comments

0

Here is my simple solution using DOMParser

 let parser = new DOMParser();
 let doc = parser.parseFromString(yourTextHtmlString, 'text/html');
 element.append(doc.body)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.