26

OK, I'm new at JavaScript, but I'm trying to change the innerHTML of a div element. Here is my script that is not working:

<head>
<script type="text/javascript">
function var1() {
document.getElementById('test').innerHTML = 'hi';
}
window.onLoad = var1();
</script>
</head>
<body>
<div id="test">change</div>
</body>

It should work, but for some reason it does not, any help?

2
  • The phrase "not working" covers an awful lot of scenarios. What exactly is the error? Commented Feb 12, 2010 at 20:22
  • theres no error, its just not changing the text inside the 'test' div. i just tried and if i put the javascript under the div, it works. is there a way to make it work when above the div as i have shown? Commented Feb 12, 2010 at 20:23

7 Answers 7

28

Rather than assigning var1 to window.onload, you're currently calling the function and storing its result. Also, this might be obvious, but var1 seems like an odd name for a function. Try this:

function var1() {
  document.getElementById('text').innerHTML = 'hi';
}

window.onload = var1;

Note the casing of onload, as well as the missing parentheses after var1.

Sign up to request clarification or add additional context in comments.

2 Comments

you should not use innerHTML it is non-standard and a bad practice. It’s a lot safer to use DOM methods like createElement, createTextNode and appendChild.
make sure it is "HTML" is capitalized
6

Correct is:

window.onload = var1;

In your example value of window.onload is undefined, because function call var1() returns nothing (undefined). Instead you should set the onload property to function (pointer) var1.

1 Comment

@David: Notice also the change from onLoad to onload. I tried it and it works just fine.
4

Using .innerHTML is non-standard, and a terrible practice for many reasons. You should create a new element using the proper standard methods and and add it to the tree where you want it.

4 Comments

No. Using innerHTML is the fastest way.
doesn't mean is isn't bad practice
It's non standard, therefore it can lead to undefined behaviour, ie: security flaws, thus is bad practice. I can't believe someone voted this down...
Just to add some context to the "fastest" comment by Josh S. stackoverflow.com/questions/18393981/…
3

Below is another simpler version

<body>
 <div id="test">change</div>
  <script type="text/javascript">
   document.getElementById('test').innerHTML = 'hi';
 </script>
</body>

Comments

1

You're getting an element with an id of "test", but there is no element with that id in your html. There is, however, one called "text".

2 Comments

oh sorry, i fixed that i didnt copy and paste from my text just typed it in here, my bad. but yeh still doesnt work
Actually this "answer" should have been a comment as it does not fix the problem(s).
1

Your example will work if you change the uppercase "L" to a lowercase "L" in "onLoad" and remove the parenthesis after var1 where you currently have window.onLoad = var1();

1 Comment

While correct, an answer should also include some explanation (as e.g. stackoverflow.com/a/2254829/6607497 does).
0

Try changing onLoad to onload and remove the parentheses () after var1:

function var1() {
  document.getElementById('test').innerHTML = 'hi';
}
window.onload = var1; // onload

3 Comments

It’s a lot safer to use DOM methods like createElement, createTextNode and appendChild.
You can check that this will not solve the problem. You execute the function before the onload, which is unintended.
@brunoais updated. the issue I was trying to draw attention to there was the camelCase on onload.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.