Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
createlink and unlink in contenteditable div

I am trying to create a text/html editor in javascript/Angular in which I am trying to create link and unlink a selected text from the contenteditable div.

here is my JSFiddle

function saveSelection() {
  if (window.getSelection) {
    const sel = window.getSelection();
    if (sel.getRangeAt && sel.rangeCount) {
      var ranges = [];
      for (var i = 0, len = sel.rangeCount; i < len; ++i) {
        ranges.push(sel.getRangeAt(i));
      }
      return ranges;
    }
    return null;
  }
}

function restoreSelection(savedSel) {
  if (savedSel) {
    if (window.getSelection) {
      const sel = window.getSelection();
      sel.removeAllRanges();
      for (var i = 0, len = savedSel.length; i < len; ++i) {
        sel.addRange(savedSel[i]);
      }
    }
  }
}

function insertLink() {
  var savedSel = saveSelection();
  var url = document.getElementById("url").value;
  restoreSelection(savedSel);
  document.execCommand("createLink", false, url);
}
.editor {
  border: 2px solid black;
  width: 200px;
  height: 50px;
}
<div contenteditable="true" class="editor"></div>
<input type="text" id="url" style="border: solid blue 1px" value=" http://" onblur="insertLink()" />

I am trying to createlink on blur of input but document.execCommand not running in the input .

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

let savedSel=''
function saveSelection() {
  if (window.getSelection) {
    const sel = window.getSelection();
    if (sel.getRangeAt && sel.rangeCount) {
      var ranges = [];
      for (var i = 0, len = sel.rangeCount; i < len; ++i) {
        ranges.push(sel.getRangeAt(i));
      }
      savedSel=ranges;
      return;
    }
    savedSel=null;
  }
}

function restoreSelection() {
  if (savedSel) {
    if (window.getSelection) {
      const sel = window.getSelection();
      sel.removeAllRanges();
      for (var i = 0, len = savedSel.length; i < len; ++i) {
        sel.addRange(savedSel[i]);
      }
    }
  }
}
function insertLink(){
  var url = document.getElementById("url").value;
  restoreSelection(savedSel);
  document.execCommand("createLink", false, url);
}
.editor {
  border: 2px solid black;
  width: 200px;
  height: 50px;
}
<div contenteditable="true" class="editor" onblur="saveSelection()"></div>
<input type="text" id="url" style="border: solid blue 1px" value=" http://" onblur="insertLink()" />

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!