Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  116] [ 1]  / answers: 1 / hits: 28848  / 11 Years ago, wed, february 27, 2013, 12:00:00

I want to print out a message in the page's <div> element upon page load. I have the following HTML and JavaScript code:



<body onload=printMsg()>
<div id=write></div>
</body>

function printMsg() {
var node = document.getElementById(write);
node.innerHTML = <p> + Hello World! + </p>;
}


Here I used onload event in <body> element to call printMsg() upon page load.



But if I don't use onload event, is there any alternative way to write to <div id=write></div> directly within the JavaScript function printMsg()?


More From » html

 Answers
13

as Whipple suggested you can do it as following :



   <body>
<div id=write></div>
<script>
(function printMsg() {
var node = document.getElementById(write);
node.innerHTML = <p> + Hello World! + </p>;
})();
</script>
</body>


DEMO


[#79957] Tuesday, February 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
vaughns

Total Points: 20
Total Questions: 112
Total Answers: 112

Location: Falkland Islands
Member since Mon, Jul 13, 2020
4 Years ago
;