I have a textarea in HTML page (page.html) and it has JavaScript in it, so what I want is, through JavaScript I could add the text in another HTML page (page2.html) written in the textarea of page1.html. How can it be done?
-
Have you done any work around this? If yes, can you add them here?Ima Miri– Ima Miri2015-08-21 13:49:40 +00:00Commented Aug 21, 2015 at 13:49
-
Try adding the value to the query string then access that string on the other pageDumisani– Dumisani2015-08-21 13:49:42 +00:00Commented Aug 21, 2015 at 13:49
-
Examples HereDumisani– Dumisani2015-08-21 13:58:13 +00:00Commented Aug 21, 2015 at 13:58
2 Answers
To do this, you must first create a new text node. This statement creates a new text node with the text "this is a test":
var node=document.createTextNode("this is a test");
Next, you can add this node to the document. To do this, you use the appendChild method. The text can be added to any element that can contain text, but we will use a paragraph. The following statement adds the text node defined above to the paragraph with the identifier p1:
document.getElementById("p1").appendChild(node);
Comments
You will need some kind of back-end. page.html sends ajax request to backend with updated value, page2.html sends ajax request to backend to get that value. If you don't know any back-end language (node.js, php for example), it will be hard to achieve this.
Another case if for example page2.html is "iframed" into page.html, then you can access directly with Javascript.