O'Reilly Forums: Exercise Not Working - O'Reilly Forums
Page 1 of 1
Exercise Not Working
#1
Posted 31 January 2012 - 09:03 AM
hi I'm doing the exercise on page 26 but it 's not working. here 's the code. Can someone please help me?
<html>
<head>
<title>iRock - The Virtual Pet Rock</title>
<script type="text/javascript">
function touchRock() {
var userName = prompt("what 's your name?");
if (userName) {
alert("HOWDY," + userName + "?");
document.getElementById("rocking").src = "../assets/rock_happy.png";
}
}
</script>
</head>
<body onload="alert('hey, what 's up baby!');">
<div style="margin-top:100px; text-align:center">
<img id="rockImg" src="../assets/rock.png" alt="iRock" style="cursor:pointer" onclick-"touchRock();" />
</div>
</body>
</html>
<html>
<head>
<title>iRock - The Virtual Pet Rock</title>
<script type="text/javascript">
function touchRock() {
var userName = prompt("what 's your name?");
if (userName) {
alert("HOWDY," + userName + "?");
document.getElementById("rocking").src = "../assets/rock_happy.png";
}
}
</script>
</head>
<body onload="alert('hey, what 's up baby!');">
<div style="margin-top:100px; text-align:center">
<img id="rockImg" src="../assets/rock.png" alt="iRock" style="cursor:pointer" onclick-"touchRock();" />
</div>
</body>
</html>
#2
Posted 31 January 2012 - 02:52 PM
if (userName) {
alert("HOWDY," + userName + "?");
document.getElementById("rocking").src = "../assets/rock_happy.png";
}
Your html doesn't have any elements with id="rocking". You do have one with id="rockImg".
#3
Posted 31 January 2012 - 05:37 PM
I changed it but it still won't work.
<html>
<head>
<title>iRock - The Virtual Pet Rock</title>
<script type="text/javascript">
function touchRock() {
var userName = prompt("what is your name?");
if (userName) {
alert("want some," + userName + "?");
document.getElementById("rockImg").src = "../assets/rock_happy.png";
}
}
</script>
</head>
<body onload="alert('hey,');">
<div style="margin-top:100px; text-align:center">
<img id="rockImg" src="../assets/rock.png" alt="iRock" style="cursor:pointer" onclick-"touchRock();" />
</div>
</body>
</html>
<html>
<head>
<title>iRock - The Virtual Pet Rock</title>
<script type="text/javascript">
function touchRock() {
var userName = prompt("what is your name?");
if (userName) {
alert("want some," + userName + "?");
document.getElementById("rockImg").src = "../assets/rock_happy.png";
}
}
</script>
</head>
<body onload="alert('hey,');">
<div style="margin-top:100px; text-align:center">
<img id="rockImg" src="../assets/rock.png" alt="iRock" style="cursor:pointer" onclick-"touchRock();" />
</div>
</body>
</html>
#5
Posted 01 February 2012 - 06:34 AM
I'm sorry in the book it looked like a dash not an equal sign. I changed it and now I'm getting the what 's your name part but I don't get a textbox to enter a name.
#7
Posted 01 February 2012 - 11:04 AM
No, when the page first displays I get the rock image and the prompt asking for your name. after hitting ok I just get the image of the rock on the screen.
#8
Posted 01 February 2012 - 11:34 AM
According to the code you posted, when the page loads you get an alert box that simply says "hey,". When you click on OK, you see the rock. You then click on the rock to get the prompt box.
If that is not happening then post your current code.
If that is not happening then post your current code.
#9
Posted 02 February 2012 - 07:32 PM
it 's working now. The book didn't make it clear that you were suppose to click on the rock the second time.
Share this topic:
Page 1 of 1



















