function displayAndPrint() {
var divContents = document.getElementById("box2text").innerHTML;
var popup = window.open('', '', 'height=500, width=500');
popup.document.write('<html>');
popup.document.write('<body > <h1>Div contents are <br>');
popup.document.write(divContents);
popup.document.write('</body></html>');
popup.document.close();
popup.print();
}
This code snippet includes a function that retrieves the content of a div and opens it in a new window for printing.
<div class="box2" id="box2">
<textarea name="box2text" id="box2text" cols="30" rows="10"></textarea>
</div>
<div class="button">
<button onclick="displayAndPrint()">DISPLAY AND PRINT</button>
</div>
Please note that this script only displays text, not CSS properties like color.