zjmiller
zjmiller

Reputation: 2787

Javascript Question: Misunderstand either getElementbyID or innerHTML

Can anyone tell me why, with the following code, when I click "Click Here", the text doesn't change to "test"?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div id="thisdiv" onClick="document.getElementbyID('thisdiv').innerHTML='test'">Click Here</div>
</body>
</html>

Upvotes: 1

Views: 186

Answers (2)

Mike Hofer
Mike Hofer

Reputation: 17002

JavaScript is case-sensitive, as Patrick pointed out. And, let's be honest: Intellisense for JavaScript pretty much blows. Be very careful when writing script.

What likely happened is that when you attempted to invoke the method with the wrong case, JavaScript couldn't find it on the inheritance change and created it instead, with the wrong case. That would explain why you got no script error, and nothing happened.

Upvotes: 0

user113716
user113716

Reputation: 322492

You have a lowercase b and an uppercase D.

 //-------v------should be uppercase
getElementById
 //----------^---should be lowercase

Example: http://jsfiddle.net/qSEpF/


But simpler would be to reference the current element with this:

onClick="this.innerHTML='test'"

Example: http://jsfiddle.net/qSEpF/1/

Upvotes: 4

Related Questions