Reputation: 15
I have a div
that contains some input
fields whose values are provided with jQuery.
Also I copied some code I found to print them, but when I press the button to print, the values from the fields are empty.
Here is my code:
<script>
$(document).ready(function() {
$('#name').val("Bruce");
$('#lastname').val("Lee");
$('#age').val("35");
});
</script>
<script>
$(document).ready(function() {
$("#printhem").click(function(){
var divContents = $("#printedDiv").html();
var printWindow = window.open('', '', 'height=700,width=900');
printWindow.document.write('<html><head><title>PRINTED</title>');
printWindow.document.write('</head><body >');
printWindow.document.write(divContents);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.print();
});
});
</script>
<div id="printedDiv"
<input name="name" type="text" id="name">
<input name="lastname" type="text" id="lastname">
<input name="age" type="text" id="age">
<div id="printhem">Print</div>
</div>
Upvotes: 0
Views: 134
Reputation: 590
Fiddle check this may be it will helpful
<div id="printedDiv">
<input name="name" type="text" id="name">
<input name="lastname" type="text" id="lastname">
<input name="age" type="text" id="age">
</div>
<input type="button" value="Print Div Contents" id="prinThm" />
$("#prinThm").click(function(){
var divContents = $("#name").val();
var divContents2 = $("#lastname").val();
var divContents3= $("#age").val();
var printWindow = window.open('', '', 'height=700,width=900');
printWindow.document.write('<html><head<title>PRINTED</title>');
printWindow.document.write('</head><body >');
printWindow.document.write('<br/>');
printWindow.document.write(divContents);
printWindow.document.write('<br/>');
printWindow.document.write(divContents2);
printWindow.document.write('<br/>');
printWindow.document.write(divContents3);
printWindow.document.write('<br/>');
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.print();
});
Upvotes: 0
Reputation: 32354
Use attr()
$(document).ready(function() {
$('#name').attr('value',"Bruce");
$('#lastname').attr('value',"Lee");
$('#age').attr('value',"35");
});
Upvotes: 1