Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textarea print (ONLY) 1

Status
Not open for further replies.

codeone

Programmer
Mar 25, 2003
343
US
is it possible to print just the value of a textarea instead of using css to filter out everything but the textarea...

I just want to print the contents of the textarea and thats it...

thanks

co
 
A couple of options with a couple of problems.

1. The first way is to launch a popup window, and write the contents of the textarea to the window as plain text ("text/plain"). Unfortunately, the browser will treat a really long paragraph that is "wrapped" by the textarea, as a really long line... and the scroll bar will appear at the bottom of the window. Not sure if this is really a problem, since the printer may wrap it for you.
And the code:
Code:
<script type="text/javascript">
function printTextArea() {
	printWin = window.open("about:blank","_blank","width=760,height=500");
	printWin.document.open("text/plain");
	var val=document.theForm.textmsg.value;
	printWin.document.write(val);
	printWin.document.close();
	if(window.print) printWin.print();
	else printWin.alert("Use the menu to print the page or CTRL-P");
}
</script>
<form name="theForm">
<textarea name="textmsg" cols="50" rows="30"></textarea>
<input value="Print" type="button" onclick="printTextArea()">
</form>

2. Write the contents out into the "body" of a new window as HTML ("html/text"). Unfortunately, any HTML tags you may have inserted will be interpretted by the browser. Hence a [ignore][/ignore] will be treated as the start of italics... just like Tek-Tips does. ;-)

3. **RECOMMENDED METHOD** Create a new textarea and write the contents directly into the textarea... creating a direct one-to-one mapping. You'll have to write out the HTML document, a form, and the textarea itself (including it's attributes). And the code:
Code:
<script type="text/javascript">
function printTextArea(objname) {
	printWin = window.open("about:blank","_blank","width=760,height=500");
	printWin.document.open("html/text");
	var theObj=document.theForm[objname];
	var newdoc="<html><body><form><textarea ";
	newdoc+="cols=\"" + theObj.cols + "\" rows=\"" + theObj.rows + "\">";
	newdoc+=theObj.value+"</textarea></form></body></html>";
	printWin.document.write(newdoc);
	printWin.document.close();
	if(window.print) printWin.print();
	else printWin.alert("Use the menu to print the page or CTRL-P");
}
</script>
<form name="theForm">
<textarea name="textmsg" cols="50" rows="30"></textarea>
<input value="Print" type="button" onclick="printTextArea('textmsg')">
</form>

Hope this helps.

Pete.


Web Developer &amp; Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Hey thanks alot pete:

I saw something like this before, looks pretty easy. Too bad we can't make extended languages like in dos to add more commands.

Anyway,

thanks alot for taking your time out to getme that example and explaing it so well, I always appreciate help.

regards

co
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top