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

186
Views
How to keep a getSelection active using javascript

I am trying to change font-size of the selected text in my div by clicking a button but when I press my button i lose the highlight/selection. How can I keep the text highlighted/selected while I also press my button.

    function sizeup(event) {
        event.preventDefault();
        var selectedText = document.getSelection();
        //please compelete after here😒😒😒😒😒
        
    }
<!--Html here-->
<div contenteditable="true" style="height:200px;">
<button onclick="sizeup(event)">SIZEUP</button>

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

0

You can select the element by its ID and then manipulate the font-size :

function changeFontSize() {
    var selectedText = "";
    if (window.getSelection) {
      selectedText = window.getSelection().toString();
      var newContent = document.getElementById('myDiv').innerHTML.replace(selectedText, '<span style="font-size:30px;">'+selectedText+'</span>');
      document.getElementById('myDiv').innerHTML = newContent;
    }
}
<div id="myDiv" contenteditable="true" style="height:100px;">asdasd</div>
<button onclick="changeFontSize()">SIZEUP</button>

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!