selected text and Highlight with javascript in textarea and div at the same time
this demo just shows the selected text in div, but I need to show the same text in both books, just show what text is Highlighted in div or textarea at the same time
<!DOCTYPE html>
<html>
<body>
<p id="test">This example uses the addEventListener() method to attach a "select" event to an element.</p>
<input style="width:100%" type="text" value="Only text in the input field is being displayed when selected!" id="myText">
<p id="demo"></p>
<script>
document.addEventListener("mouseup", function(){
if(document.getSelection()){
document.querySelector("#demo").textContent = document.getSelection();
}else if(window.getSelection()){
document.querySelector("#demo").textContent = document.getSelection();
}else if(document.selection){
document.querySelector("#demo").textContent = document.selection.createRange().text;
}
});
</script>
</body>
</html>
chek my screenshot
I just need to show the text what is selected in textarea or div need to change color in both blocks at the same time
also, my textarea don't have id or class - and I want to keep it this way
<textarea></textarea>
thank you - if you can show a javascript example will be very helpful.
you can select textarea with:
document.querySelector("textarea").style.backgroundColor = "yellow";
window.getSelection() has all the properties to count offset of the selection from the entire text.