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>









share|improve this question
























  • 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















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>









share|improve this question
























  • 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













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>









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer





















  • 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




















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>








share|improve this answer





















  • 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


















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;
}





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer





















  • 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

















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.






share|improve this answer





















  • 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















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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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














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>








share|improve this answer





















  • 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















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>








share|improve this answer





















  • 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













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>








share|improve this answer












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>






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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










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;
}





share|improve this answer





















  • 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















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;
}





share|improve this answer





















  • 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













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;
}





share|improve this answer












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;
}






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...