0

Possible Duplicate:
javascript - document.write error?

I want to write html into javascript. I am sure this question, has been asked ofter, I know its simple but I can't figure it out.

I tried this:

  <script>
  document.write("<h1>This is a heading</h1>");
  document.write("<p>This is a paragraph</p>");
  </script>

But it removes all the other html.

I know this is noob question, I pretty unexperienced at javascript...

Community
  • 1
  • 1
Hamada Badran
  • 139
  • 2
  • 2
  • 8
  • You can use [innerHTML](https://developer.mozilla.org/en-US/docs/DOM/element.innerHTML) and/or [createElement](https://developer.mozilla.org/en-US/docs/DOM/document.createElement) – Danilo Valente Jan 11 '13 at 03:20
  • 1
    When you get the hang of js this is my favorite: [http://beebole.com/pure/](http://beebole.com/pure/) – Kyle Weller Jan 11 '13 at 03:24

2 Answers2

0
document.body.innerHTML += '<h1>This is a heading</h1>';
document.body.innerHTML += '<p>This is a paragraph</p>';
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

Lets say i have a div on my page and i want to add content to it:

<div id="myDiv"><div>

I can use javascript to do so:

<script>
var div = document.getElementById("myDiv");
var myHtmlString = "<h1>This is a heading</h1>";
myHtmlString += "<p>This is a paragraph</p>"
div.innerHTML = myHtmlString;
</script>
Ibu
  • 42,752
  • 13
  • 76
  • 103