print div with textarea incl. user input using js
up vote
0
down vote
favorite
I have this simple script that prints a part of a webpage.
However, it does not print user input.
My goal is to only print the form and the user input; not the rest of the webpage.
I do not want to use css; just js/jQuery and html.
My question is: how can I print various form input elements including the user input?
HTML:
<div id="print-content">
<textarea>Print User Input</textarea>
<input type="button" onclick="printDiv('print-content')" value="print a div!"/>
</div>
js:
<script type="text/javascript">
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
javascript jquery printing
add a comment |
up vote
0
down vote
favorite
I have this simple script that prints a part of a webpage.
However, it does not print user input.
My goal is to only print the form and the user input; not the rest of the webpage.
I do not want to use css; just js/jQuery and html.
My question is: how can I print various form input elements including the user input?
HTML:
<div id="print-content">
<textarea>Print User Input</textarea>
<input type="button" onclick="printDiv('print-content')" value="print a div!"/>
</div>
js:
<script type="text/javascript">
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
javascript jquery printing
Could you explain a little more about what you're trying to do here? Also, there is not need for the form tags, so I'll edit them out and remove the tags.
– cmprogram
Nov 21 at 9:43
@cmprogram: I am just trying to print a form with user input. Goal is to only print the form; not the rest of the webpage.
– Els den Iep
Nov 21 at 10:16
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this simple script that prints a part of a webpage.
However, it does not print user input.
My goal is to only print the form and the user input; not the rest of the webpage.
I do not want to use css; just js/jQuery and html.
My question is: how can I print various form input elements including the user input?
HTML:
<div id="print-content">
<textarea>Print User Input</textarea>
<input type="button" onclick="printDiv('print-content')" value="print a div!"/>
</div>
js:
<script type="text/javascript">
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
javascript jquery printing
I have this simple script that prints a part of a webpage.
However, it does not print user input.
My goal is to only print the form and the user input; not the rest of the webpage.
I do not want to use css; just js/jQuery and html.
My question is: how can I print various form input elements including the user input?
HTML:
<div id="print-content">
<textarea>Print User Input</textarea>
<input type="button" onclick="printDiv('print-content')" value="print a div!"/>
</div>
js:
<script type="text/javascript">
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
javascript jquery printing
javascript jquery printing
edited Nov 21 at 12:27
asked Nov 21 at 9:39
Els den Iep
190111
190111
Could you explain a little more about what you're trying to do here? Also, there is not need for the form tags, so I'll edit them out and remove the tags.
– cmprogram
Nov 21 at 9:43
@cmprogram: I am just trying to print a form with user input. Goal is to only print the form; not the rest of the webpage.
– Els den Iep
Nov 21 at 10:16
add a comment |
Could you explain a little more about what you're trying to do here? Also, there is not need for the form tags, so I'll edit them out and remove the tags.
– cmprogram
Nov 21 at 9:43
@cmprogram: I am just trying to print a form with user input. Goal is to only print the form; not the rest of the webpage.
– Els den Iep
Nov 21 at 10:16
Could you explain a little more about what you're trying to do here? Also, there is not need for the form tags, so I'll edit them out and remove the tags.
– cmprogram
Nov 21 at 9:43
Could you explain a little more about what you're trying to do here? Also, there is not need for the form tags, so I'll edit them out and remove the tags.
– cmprogram
Nov 21 at 9:43
@cmprogram: I am just trying to print a form with user input. Goal is to only print the form; not the rest of the webpage.
– Els den Iep
Nov 21 at 10:16
@cmprogram: I am just trying to print a form with user input. Goal is to only print the form; not the rest of the webpage.
– Els den Iep
Nov 21 at 10:16
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
The current value of an input is not represented in the HTML (only the default value), so it won't show up in innerHTML
.
You would need to loop over all the inputs in the original page, read their value
properties, then copy the values to the new inputs on the new page.
You'd probably be better off using CSS and @media print
to hide the elements you don't want to be printed rather than rewriting the document with innerHTML
multiple times.
Thanks, but problem is I don't want to use css. Only js/jQuery and html
– Els den Iep
Nov 21 at 10:12
@ElsdenIep – Then follow the advice I gave in the second paragraph of the answer.
– Quentin
Nov 21 at 10:13
That is a bit too complicated for me. Do you have an example that I could alter for my purpose?
– Els den Iep
Nov 21 at 10:18
add a comment |
up vote
1
down vote
Try by removing this line document.body.innerHTML = printContents;
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
window.print();
}
<div id="print-content">
<form>
<textarea>Print User Input</textarea>
<input type="button" onclick="printDiv('print-content')" value="print a div!" />
</form>
</div>
Problem is: My script only prints content of id="print-content". Your script prints the whole page.
– Els den Iep
Nov 21 at 10:11
add a comment |
up vote
1
down vote
Tested on chrome only.
function printDiv(divName) {
var printContents = document.getElementById(divName).cloneNode(true);
w = window.open();
w.document.body.appendChild(printContents);
w.print();
w.close();
return true;
}
Can't believe it is that simple: your script works great for me. Thanks!
– Els den Iep
Nov 21 at 11:39
Works great in Chrome, FF, Opera, Android. Not working in Edge and IE.
– Els den Iep
Nov 21 at 12:22
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The current value of an input is not represented in the HTML (only the default value), so it won't show up in innerHTML
.
You would need to loop over all the inputs in the original page, read their value
properties, then copy the values to the new inputs on the new page.
You'd probably be better off using CSS and @media print
to hide the elements you don't want to be printed rather than rewriting the document with innerHTML
multiple times.
Thanks, but problem is I don't want to use css. Only js/jQuery and html
– Els den Iep
Nov 21 at 10:12
@ElsdenIep – Then follow the advice I gave in the second paragraph of the answer.
– Quentin
Nov 21 at 10:13
That is a bit too complicated for me. Do you have an example that I could alter for my purpose?
– Els den Iep
Nov 21 at 10:18
add a comment |
up vote
2
down vote
The current value of an input is not represented in the HTML (only the default value), so it won't show up in innerHTML
.
You would need to loop over all the inputs in the original page, read their value
properties, then copy the values to the new inputs on the new page.
You'd probably be better off using CSS and @media print
to hide the elements you don't want to be printed rather than rewriting the document with innerHTML
multiple times.
Thanks, but problem is I don't want to use css. Only js/jQuery and html
– Els den Iep
Nov 21 at 10:12
@ElsdenIep – Then follow the advice I gave in the second paragraph of the answer.
– Quentin
Nov 21 at 10:13
That is a bit too complicated for me. Do you have an example that I could alter for my purpose?
– Els den Iep
Nov 21 at 10:18
add a comment |
up vote
2
down vote
up vote
2
down vote
The current value of an input is not represented in the HTML (only the default value), so it won't show up in innerHTML
.
You would need to loop over all the inputs in the original page, read their value
properties, then copy the values to the new inputs on the new page.
You'd probably be better off using CSS and @media print
to hide the elements you don't want to be printed rather than rewriting the document with innerHTML
multiple times.
The current value of an input is not represented in the HTML (only the default value), so it won't show up in innerHTML
.
You would need to loop over all the inputs in the original page, read their value
properties, then copy the values to the new inputs on the new page.
You'd probably be better off using CSS and @media print
to hide the elements you don't want to be printed rather than rewriting the document with innerHTML
multiple times.
answered Nov 21 at 9:45
Quentin
634k718551024
634k718551024
Thanks, but problem is I don't want to use css. Only js/jQuery and html
– Els den Iep
Nov 21 at 10:12
@ElsdenIep – Then follow the advice I gave in the second paragraph of the answer.
– Quentin
Nov 21 at 10:13
That is a bit too complicated for me. Do you have an example that I could alter for my purpose?
– Els den Iep
Nov 21 at 10:18
add a comment |
Thanks, but problem is I don't want to use css. Only js/jQuery and html
– Els den Iep
Nov 21 at 10:12
@ElsdenIep – Then follow the advice I gave in the second paragraph of the answer.
– Quentin
Nov 21 at 10:13
That is a bit too complicated for me. Do you have an example that I could alter for my purpose?
– Els den Iep
Nov 21 at 10:18
Thanks, but problem is I don't want to use css. Only js/jQuery and html
– Els den Iep
Nov 21 at 10:12
Thanks, but problem is I don't want to use css. Only js/jQuery and html
– Els den Iep
Nov 21 at 10:12
@ElsdenIep – Then follow the advice I gave in the second paragraph of the answer.
– Quentin
Nov 21 at 10:13
@ElsdenIep – Then follow the advice I gave in the second paragraph of the answer.
– Quentin
Nov 21 at 10:13
That is a bit too complicated for me. Do you have an example that I could alter for my purpose?
– Els den Iep
Nov 21 at 10:18
That is a bit too complicated for me. Do you have an example that I could alter for my purpose?
– Els den Iep
Nov 21 at 10:18
add a comment |
up vote
1
down vote
Try by removing this line document.body.innerHTML = printContents;
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
window.print();
}
<div id="print-content">
<form>
<textarea>Print User Input</textarea>
<input type="button" onclick="printDiv('print-content')" value="print a div!" />
</form>
</div>
Problem is: My script only prints content of id="print-content". Your script prints the whole page.
– Els den Iep
Nov 21 at 10:11
add a comment |
up vote
1
down vote
Try by removing this line document.body.innerHTML = printContents;
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
window.print();
}
<div id="print-content">
<form>
<textarea>Print User Input</textarea>
<input type="button" onclick="printDiv('print-content')" value="print a div!" />
</form>
</div>
Problem is: My script only prints content of id="print-content". Your script prints the whole page.
– Els den Iep
Nov 21 at 10:11
add a comment |
up vote
1
down vote
up vote
1
down vote
Try by removing this line document.body.innerHTML = printContents;
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
window.print();
}
<div id="print-content">
<form>
<textarea>Print User Input</textarea>
<input type="button" onclick="printDiv('print-content')" value="print a div!" />
</form>
</div>
Try by removing this line document.body.innerHTML = printContents;
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
window.print();
}
<div id="print-content">
<form>
<textarea>Print User Input</textarea>
<input type="button" onclick="printDiv('print-content')" value="print a div!" />
</form>
</div>
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
window.print();
}
<div id="print-content">
<form>
<textarea>Print User Input</textarea>
<input type="button" onclick="printDiv('print-content')" value="print a div!" />
</form>
</div>
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
window.print();
}
<div id="print-content">
<form>
<textarea>Print User Input</textarea>
<input type="button" onclick="printDiv('print-content')" value="print a div!" />
</form>
</div>
answered Nov 21 at 9:48
brk
25k31939
25k31939
Problem is: My script only prints content of id="print-content". Your script prints the whole page.
– Els den Iep
Nov 21 at 10:11
add a comment |
Problem is: My script only prints content of id="print-content". Your script prints the whole page.
– Els den Iep
Nov 21 at 10:11
Problem is: My script only prints content of id="print-content". Your script prints the whole page.
– Els den Iep
Nov 21 at 10:11
Problem is: My script only prints content of id="print-content". Your script prints the whole page.
– Els den Iep
Nov 21 at 10:11
add a comment |
up vote
1
down vote
Tested on chrome only.
function printDiv(divName) {
var printContents = document.getElementById(divName).cloneNode(true);
w = window.open();
w.document.body.appendChild(printContents);
w.print();
w.close();
return true;
}
Can't believe it is that simple: your script works great for me. Thanks!
– Els den Iep
Nov 21 at 11:39
Works great in Chrome, FF, Opera, Android. Not working in Edge and IE.
– Els den Iep
Nov 21 at 12:22
add a comment |
up vote
1
down vote
Tested on chrome only.
function printDiv(divName) {
var printContents = document.getElementById(divName).cloneNode(true);
w = window.open();
w.document.body.appendChild(printContents);
w.print();
w.close();
return true;
}
Can't believe it is that simple: your script works great for me. Thanks!
– Els den Iep
Nov 21 at 11:39
Works great in Chrome, FF, Opera, Android. Not working in Edge and IE.
– Els den Iep
Nov 21 at 12:22
add a comment |
up vote
1
down vote
up vote
1
down vote
Tested on chrome only.
function printDiv(divName) {
var printContents = document.getElementById(divName).cloneNode(true);
w = window.open();
w.document.body.appendChild(printContents);
w.print();
w.close();
return true;
}
Tested on chrome only.
function printDiv(divName) {
var printContents = document.getElementById(divName).cloneNode(true);
w = window.open();
w.document.body.appendChild(printContents);
w.print();
w.close();
return true;
}
answered Nov 21 at 10:54
Sven Liivak
76718
76718
Can't believe it is that simple: your script works great for me. Thanks!
– Els den Iep
Nov 21 at 11:39
Works great in Chrome, FF, Opera, Android. Not working in Edge and IE.
– Els den Iep
Nov 21 at 12:22
add a comment |
Can't believe it is that simple: your script works great for me. Thanks!
– Els den Iep
Nov 21 at 11:39
Works great in Chrome, FF, Opera, Android. Not working in Edge and IE.
– Els den Iep
Nov 21 at 12:22
Can't believe it is that simple: your script works great for me. Thanks!
– Els den Iep
Nov 21 at 11:39
Can't believe it is that simple: your script works great for me. Thanks!
– Els den Iep
Nov 21 at 11:39
Works great in Chrome, FF, Opera, Android. Not working in Edge and IE.
– Els den Iep
Nov 21 at 12:22
Works great in Chrome, FF, Opera, Android. Not working in Edge and IE.
– Els den Iep
Nov 21 at 12:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409105%2fprint-div-with-textarea-incl-user-input-using-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you explain a little more about what you're trying to do here? Also, there is not need for the form tags, so I'll edit them out and remove the tags.
– cmprogram
Nov 21 at 9:43
@cmprogram: I am just trying to print a form with user input. Goal is to only print the form; not the rest of the webpage.
– Els den Iep
Nov 21 at 10:16