Click here to Skip to main content
15,896,545 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

Can any body tell me how can i write some text to html file using javascript

thanks in advance
Posted

There are a number of methods. You can write whole document, etc., but I think most flexible method is modification of properties of existing elements.

Here is the idea: you get some element by its Id and then assign some string to its property innerHtml.

For example:
HTML
<html>
    <head>
        <title>Writing HTML by JavaScript</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript"><!--
            function writeHtml() {
                var div = document.getElementById("placeholder");
                div.innerHTML = "<h2>This is <i><big>generated</big></i> code!</h2>";
            }
        --></script>
    </head>
<body>

<button onClick="writeHtml();">Try it!</button>
<div id="placeholder" />

</body>
</html>


—SA
 
Share this answer
 
v2
Comments
Toniyo Jackson 18-Oct-11 2:53am    
Better than my answer, 5!
Sergey Alexandrovich Kryukov 18-Oct-11 2:55am    
Thank you very much, Toniyo.
--SA
Uday P.Singh 18-Oct-11 3:09am    
my 5!
Sergey Alexandrovich Kryukov 18-Oct-11 11:21am    
Thank you, Uday.
--SA
Try this in Javascript.
JavaScript
document.write("Your text here")


More info here[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Oct-11 2:52am    
Correct, my 5, but I think I suggested more flexible method, please see.
--SA
Toniyo Jackson 18-Oct-11 2:56am    
Thanks SA. Your answer is best. I voted & commented before seeing this :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900