nr.iras.sk
nr.iras.sk

Reputation: 8498

Why TypeError: document.getElementById() is null

Why the following code generates TypeError: document.getElementById("docPrint") is null

var printwindow = window.open('', '', 'fullScreen=no');
printwindow.document.write('<iframe id="docPrint" width="100%" height="100%" src="http://localhost:8080/hiring/docs/Keneth _1340800082258/Keneth _resume_1340800082258.pdf"></iframe>');
printwindow.self.focus();
document.getElementById('docPrint').focus();
document.getElementById('docPrint').contentWindow.print();

Upvotes: 0

Views: 5581

Answers (3)

codebox
codebox

Reputation: 20254

You need to prefix your calls to document.getElementById with 'printwindow':

printwindow.document.getElementById('docPrint').focus();
printwindow.document.getElementById('docPrint').contentWindow.print();

You might also want to keep a reference to the element in a variable, to avoid the boilerplate

var el = printwindow.document.getElementById('docPrint');
el.focus();
el.contentWindow.print();

Upvotes: 1

David G
David G

Reputation: 96845

Prependprintwindow. to each instance of document.getElementById:

printwindow.document.getElementById('docPrint').focus();
printwindow.document.getElementById('docPrint').contentWindow.print();

Upvotes: 1

Quentin
Quentin

Reputation: 944203

You are operating across two windows.

printwindow.document.write
document.getElementById

If you want to get the element you created in the popup then you have to call it's gEBI method.

printwindow.document.write
printwindow.document.getElementById

Upvotes: 3

Related Questions