0

.innerHTML is used when you trigger an event but if you just want to display the text how would you go about do it??
I don't want to used document.writeln because it will "Wipe out all the contents" of that page.
So I have some HTML text and javascript text. I would like both to display on my page.

Here's my code

<html>
<head>

<script type="text/javascript"> 
function display() {
   document.writeln("How do I make both text appear");
}
</script>
</head>

<body onload="display()">
I would like this text in the body to remain.

<script type="text/javascript">
   display();
</script>


</body>
</html>

thanks

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by LaxLoafer
0

Hiyatran,

The body onload event occurs after a document has finished loading. Attempting to write content after this has happened will invalidate the structure of your HTML document. This might explain why you're seeing a blank page.

To add additional content to a page you'll need to obtain a reference to an existing element and then modify it in some way. One way to achieve this is to do something like...

<html>
<head>
<title></title>
<script type="text/javascript">
function foobar() {
    document.getElementById('placeholder').innerHTML = "The Martians are coming!";
}
</script>
</head>
<body onload="foobar()">
<p>Hello Earthling.</p>
<p id="placeholder"></p>
</body>
</html>

The code above works by obtaining a reference to an existing 'p' element, identified here as 'placeholder', and then changing the value of its innerHTML property.

Edited by LaxLoafer: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.