Estoy tratando de cambiar el tamaño de fuente del texto seleccionado en mi div haciendo clic en un botón, pero cuando presiono mi botón pierdo el resaltado/selección. ¿Cómo puedo mantener el texto resaltado/seleccionado mientras también presiono mi botón?
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>Puede seleccionar el elemento por su ID y luego manipular el tamaño de fuente:
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>