Languages :: DHTML, JScript :: Displaying the contents of a textarea element with Javascript |
|||
| By: Baryon |
Date: 03/12/2005 17:58:56 |
Points: 20 | Status: Answered Quality : Excellent |
|
I think this is quite simple, but I can't find out how to do it. I want a bookmarklet that, when clicked, will take the contents of the textarea element on the current page and display it in isolation, on a new page perhaps. Even better would be if it could save it to the hard disk (or prompt for it to be saved). And is it possible to add it to the clipboard? Any of these methods are desirable. Thanks if you can help. |
|||
| By: Baryon | Date: 03/12/2005 18:21:17 | Type : Comment |
|
| Incidentally, it needs to work when the contents of the textarea box is markup. I don't want it to render/parse the markup. | |||
| By: VGR | Date: 03/12/2005 18:37:49 | Type : Answer |
|
| hallo, basically yes it is possible to get the contents of any HTML control. Then, saving it to disk or adding its contents to the clipboard clearly exceeds Javascript's possibilities. This usually requires either a Java Applet (althought it couldn't write to the disk) or an ActiveX ( hurgh :D ) As for getting the contents so that it can be put on a new page, you understand it means that the first DHTML page will have to issue client-side the request to the server for the second DHTML page , via a POST or GET in clear text. This can be invisible though. You may also make the first DHTML page build completely the second DHTML page on client-side via DOM "nodes" and "child" methods, but this is bold. Of course, if you agree on calling the server to build the second DHTML page, then it serves no purpose at all to get the textarea's contents in Javascript. The server can receive it in the $_QUERY[]. As for "how to get that darn textarea's contents in javascript", here's a sample : <script language="JavaScript" type="text/javascript"> <!-- var elem = document.getElementById('textarea_id'); var thecontents = elem.innerHTML; //--> </script> HTH |
|||
| By: Baryon | Date: 03/12/2005 19:16:52 | Type : Comment |
|
| Thank you. I've used your information and other sources to create this: void window.open("","","").document.write("<PRE>"+document.body.getElementsByTagName("TEXTAREA")[0].innerHTML+"<\/PRE>"); which gives the exact effect I needed. |
|||
| By: VGR | Date: 03/12/2005 19:34:51 | Type : Comment |
|
| good. Just a bit too "all code on a single line" to my taste ;-) of course, using an explicit "id" for THAT textarea is in my humble opinion safer than using a getElementsByTagName[0] which takes the first encountered, and is probably more efficient, but it's up to you regards |
|||
| By: VGR | Date: 03/12/2005 19:36:06 | Type : Comment |
|
| ah, just a mistake from me I think. I wanted to use the method that extracts the contents in "non-parsed html", and I gave you "innerHTML". I think it's "innerText" in stead. Sorry. | |||
| By: Baryon | Date: 03/12/2005 19:42:20 | Type : Comment |
|
| I am sure you're right about getElementById being safer, but unfortunately I'm using this on existing web pages that I haven't written, and the textareas don't have id attributes. And in this case, innerHTML certainly seems to be what I want. Thanks for your help. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








