1

I have an iframe on a webpage. This iframe is sourced to (that is, shows the contents of) a .txt file on my server.

I would like to implement a button (that sits outside of the iFrame, of course) that, when clicked, Selects All the text in the iFrame.

Is this possible? (For example, via jQuery/JavaScript.)

Sébastien
  • 11,860
  • 11
  • 58
  • 78
Crickets
  • 524
  • 1
  • 8
  • 23
  • The code found at the bottom of this webpage does not work: https://www.experts-exchange.com/questions/20699826/Selecting-all-text-within-an-iframe.html – Crickets Dec 22 '17 at 07:54

2 Answers2

3

there is a DOM property for iframes called contentDocument which returns the document object generated by a frame or iframe element.

HTML

<iframe id="frame" src="file.txt"></iframe>
<!-- points to the text file -->

<button id="selectText">Copy Text</button>
<!-- button to copy the text -->

JS

var selButtom = document.getElementById('selectText'); //store the button value in a variable
var frame = document.getElementById('frame'); // store iframe in variable

selButtom.addEventListener("click", function(){
    var frameContent = frame.contentDocument; // get content of iframe
    frameContent.execCommand('selectAll'); // execute select command
},false);
mac
  • 114
  • 7
0

Use iFrameName.window you can use any method of iframe page.

To select text, you can try https://code.google.com/p/rangy/ . Just simple replace all document.body to iFrameName.window.document.body.

Or a simple alternative way: just use iFrameName.window.document.execCommand("selectAll") .