Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda