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

229
Views
Creating a highlighter for Chrome Extension

I want to create a highlighter feature for my Chrome extension. I am not sure what is wrong with my code, I am using Javascript. The code is below. Note - I am using the attribute as a substitute for highlighting, it's not permanent.

function getText() {
 if (document.getSelection) {
    return document.getSelection().toString();
 } else {
    if (document.selection) {
        var text2 = document.selection.createRange();
    }
  }
}
function replaceText() {
  let tex = getText();
  if (document.getSelection) {
     alert("hi");
     document.getSelection().innerHTML = document.getSelection().replace(tex, "<u>" + 
     tex + "</u>");
  } else {
    if (document.selection) {
        document.Selection().createRange().innerHTML = tex.replace(tex, "<u>" + tex + " 
        <u>");
        alert("hello");
    }
}
    alert(tex);
}

let btn = document.createElement("button");
btn.innerHTML = "Highlight";
btn.onclick = function () {
replaceText();
};
document.body.appendChild(btn);

[Better view of the Code] 1

about 4 years ago · Juan Pablo Isaza
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!