Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-17-2013, 03:03 AM   PM User | #1
alans@uoregon.e
New to the CF scene

 
Join Date: Feb 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
alans@uoregon.e is an unknown quantity at this point
Building an Image into a function

I'm a complete beginner to javascript and I'm curious what's wrong with my code. We need to make one original image appear, and then upon clicking two images need to appear that both simulate a die throw, so randomly being 1 through 6. Please help!

Code:
<!DOCTYPE html>
<head>
	<title>More Games</title>
	<script>
function SelectImages()
// Assumes: die images are in http://pages.uoregon.edu/alans/111/CIS%20111/images/
// Results: displays a randomly selected image of a 6-sided die
{
roll1 = Math.floor(Math.random() * 6) + 1;
imgName = '../images/die' + roll1 + '.gif';
document.getElementById('dieImage').src = imgName;

var dieImg2 = roll1 = '../images/die' + roll2 + '.gif';
var myNewTag = "<img id='dieImg2' alt='die Image' src='../images/die1.gif'>"
document.getElementById('dieImage').innerHTML += myNewTag;
}


	</script>
</head>
<body>
	<div style="text-align:center">

		<h2>Doubles Simulation</h2>
		<p>
			<img id="dieImage" alt="die image" src="../images/die1.gif">

		</p>
		<input type="button" value="Click to Play" onclick="SelectImages();">
	</div>

	<hr>
	<a href="http://pages.uoregon.edu/alans/111/CIS%20111/p3/slots.html">In Class Slots Exercise</a>
	<br>
	<a href="http://pages.uoregon.edu/alans/111/CIS%20111/p3/InClassDiceStats.html">In Class Dice Counter Exercise</a>
	<br>
	<p>
		We reviewed for the Midterm Exam
	</p>

	<footer>
		<p>
			&copy; Copyright  by Alan Sylvestre
			<br>
			<a href="mailto:alans@uoregon.edu">Email Me</a>
			<br>
			<a href="http://cargocollective.com/alansylvestre">View My Website Here</a>
		</p>
	</footer>
</body>
</html>
alans@uoregon.e is offline   Reply With Quote
Old 02-17-2013, 09:21 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,855
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
you don’t roll for the second die.

and since the image (dieImage) is an <img>, it doesn’t have content you could address with innerHTML.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Tags
function, images, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:21 PM.


Advertisement
Log in to turn off these ads.