I'm trying to apply H1 on the selected text. but when I select some text it applies to all text.
All I need to apply H1 for just selected text, not for all text. I need to get selected text and replace it with an element not just to get the selected text.
function exec(){
    document.execCommand('formatblock', false, 'h1');
}#ce{
  height: 100px;
  border: solid 1px black;
  padding: 10px;
}<button onclick="exec()">apply H1</button>
<div id="ce" contenteditable></div> 
    