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

164
Views
how to toggle italic tags inside editable div?

trying to create a simple html editor
in the example below pls place cursor inside the word lorem and click button
the word becomes italic
two questions

  • is this the right way to do this ?
  • how to toggle this funcionality, i.e. remove <i></i> by clicking again ?

var ed = $('#ed');
$('.btnitalic').on('click', function(){
    var sel = window.getSelection();
    sel.modify('move', 'backward', 'word');
    sel.modify('extend', 'right', 'word');
    var str = sel.toString();
    var rn = sel.getRangeAt(0);
    var tx = rn.extractContents();
    var i = document.createElement('i');
    i.appendChild(tx);
    rn.insertNode(i);
    console.clear();
    console.log(ed.html());
});
.ed{display:inline-block; width:100%; min-height:99px; background:orange;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class='btnitalic'>ITALIC</button>
<br><br>
<div class='ed' id='ed' contenteditable>
lorem ipsum
dolor sit
</div>

about 4 years ago · Santiago Gelvez
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!