Need to insert a link (replace highlighted text)
The idea is - select lorem
- type a href inside an input - and pressing Enter - set a link.
Problem - highlighted text is not highlighted if inpa
has focus.
Any solution?
$('#inpa').on('keypress', function(e){
if(e.keyCode == 13){
let a = $('#inpa').val();
let b = window.getSelection().toString();
let ht = "<a href = '" + a + "' target = '_blank'>" + b + "</a>";
document.execCommand('insertHTML', ht);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='story' id='story'>lorem ipsum</div>
<br>
<input type='text' class='inpa' id='inpa'>