Click to See Complete Forum and Search --> : why does this code not work?
wjonesny
08-23-2006, 03:14 AM
<html>
<head>
<title>Changing faces</title>
<script type="text/javascript">
function CFaces(x)
{
if (x=1){
document.images.faces.src="bored.gif";
}
if (x=2){
document.images.faces.src="happy.gif";
}
if (x=3){
document.images.faces.src="sad.gif";
}
}
</script>
</head>
<body>
<center>
<img name="faces" src="smile_roll.gif"><br><br>
<form name="TheFaces">
<input type="submit" value=" 1 " onClick="CFaces(1);">
<input type="submit" value=" 2 " onClick="CFaces(2);">
<input type="submit" value=" 3 " onClick="CFaces(3);">
</form>
</center>
</body>
</html>
sridhar_423
08-23-2006, 03:23 AM
x==1 not x=1 and so on
wjonesny
08-23-2006, 03:42 AM
I made changes but still does not work.
sridhar_423
08-23-2006, 04:25 AM
very funny.. i too get the same problem.. no error in the code.. i tested it.. but the src is reset after returning from the function.
to cross-check this, put an alert at the ending of the function.
i too donno why this is happening. Lets see if anyone can throw some light on this.
Ultimater
08-23-2006, 04:28 AM
INPUT TYPE="button" in addition to what sridhar_423 suggested.
sridhar_423
08-23-2006, 04:31 AM
ohhh... missed tat one.
wjonesny
08-23-2006, 06:02 AM
I had tried doing everything to get it to work I forgot to change the submit back to button. I have another that needed debugging if one of you have a look see . I will post it later. maybe next time it won't be a stupid thing like = vs == :)
wjonesny
08-23-2006, 06:07 AM
<html>
<!-- newpics.html -->
<!-- This page allows the user to select images randomly. -->
<!---------------------------------------------------------------------------------->
<head>
<title> Picture Fun </title>
<script type="text/javascript" src="http://www.prenhall.com/reed/random.js">
function Ximage()
{
var x;
x = RandomInt(1, 4);
if (x == 1) {
document.images.face.src ="sad.gif";
}
if (x == 2) {
document.images.face.src ="smile_roll.gif";
}
if (x == 3) {
document.images.face.src ="bored.gif";
}
if (x == 4) {
document.images.face.src ="happy.gif";
}
}
</script>
</head>
<body>
<div style="text-align:center">
<h2>How do you feel today?</h2>
<img name="face" src="happy.gif" alt="face image" />
<br><br>
<form name="display">
<input type="button" value="I am feeling" onClick="Ximage();" />
</form>
</div>
</body>
</html>
muazzez
08-23-2006, 06:41 AM
<head>
<title> Picture Fun </title>
<script type="text/javascript" >
function Ximage()
{
var x;
x = Math.round(Math.random()*3);
if (x == 0) {
document.images.face.src ="agac.bmp";
}
if (x == 1) {
document.images.face.src ="ev1.bmp";
}
if (x == 2) {
document.images.face.src ="saat.bmp";
}
if (x == 3) {
document.images.face.src ="KUMANDA.bmp";
}
}
</script>
</head>
<body>
<div style="text-align:center">
<h2>How do you feel today?</h2>
<img name="face" src="saat.bmp" alt="face image" />
<br><br>
<form name="display">
<input type="button" value="I am feeling" onClick="Ximage();" />
</form>
</div>
</body>
</html>
Ultimater
08-23-2006, 12:42 PM
What muazzez suggested will work however your fix is as simple as:
<script type="text/javascript" src="http://www.prenhall.com/reed/random.js"></script>
<script type="text/javascript">