Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a blank window, write to it, and select the written text

Tags:

javascript

I'm trying to open a blank window with Javascript, write text to it, and then select the text that I wrote, all automatically.

I have:

var myWindow=window.open('');
myWindow.document.write("hey");
myWindow.select();

which opens the window and writes the text, but does not select it.

like image 927
user1795352 Avatar asked Dec 26 '22 15:12

user1795352


1 Answers

This should do it:

var myWindow=window.open('');
myWindow.document.write("<div id='hello'>hey<div>");
var range = myWindow.document.createRange();
range.selectNode(myWindow.document.getElementById('hello'));
myWindow.getSelection().addRange(range);
myWindow.select();
like image 62
ct_ Avatar answered Dec 29 '22 07:12

ct_