tags:

views:

47

answers:

3

Hi guys, I'm just starting to learn JavaScript and can't figure out why getElementById won't work?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The Rock</title>
<style type="text/css">
<!--
body {
    text-align: center;
}
-->
</style>

<script type="text/javascript">

function touchRock() { 
    var username = prompt("What is your name?");

    if (username) { alert("Hello, "+username+"! I am The Rock.");
    document.getElementById("rockimg").scr = "rock2.png";}
    }

</script>
</head>
<body>



<p>&nbsp;</p>
<p>&nbsp;</p>
<p><img id="rockimg" src="rock1.png" align="middle" style="cursor:pointer" onclick="touchRock();"  /></p>
</body>
</html>

The idea is that once the user click on the rock, asks for his name, greets him and changes the image. The name/greeting part works, but the image doesn't change.

Any ideas? Thanks!

+5  A: 

You got a typo. Change .scr into .src and it will work

Ghommey
THANKS! WORKS NOW!
+2  A: 

You have a typo on this line:

document.getElementById("rockimg").scr

Should be src not scr

Hooray Im Helping
+1  A: 

change

document.getElementById("rockimg").scr

to

document.getElementById("rockimg").src

(it's src, not scr)

oezi