I'm trying to print a document by using a button, but every time I try the button it doesn't work.
var printContents = document.getElementById("test").innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
And when that doesn't work I open the console and try it, but then I get this
TypeError: Cannot read property 'innerHTML' of null
I've trie creating an empty html page with nothing on it but a phrase and a button that runs the code.
function print(){
window.print();
}
But that doesn't do anything either.
edit: Isn't there a way with only JavaScript? This is the last thing I have to do for my school project but I don't know Jquery :s and the teacher said it's possible with Javascript.