Flash Games
 FAQ   Search   Memberlist   Usergroups   Register  Profile   Log in to check your private messages   Log in 


Need help to display an image when button is clicked



 

Post new topic   Reply to topic  
   DEVPPL Forum Index -> JavaScript Forum
View previous topic :: View next topic  
Author Message
Morta



Joined: 23 Apr 2008
Posts: 4

PostPosted: Wed Apr 23, 2008 2:43 am    Post subject: Need help to display an image when button is clicked Reply with quote

Hi-
I want my image to display when the button is clicked. When I put the image source in the HTML portion of the code, it always displays.
When I try to access it from the JS portion of the code, I can't.
Can someone please rewrite this code so that it will work and I will then know how to do it?
Thank you.....

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml1 transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Myimage</title>

<script type = "text/javascript">

function showImg() {                 
var myimg = document.images["image01"] //first image
}   
</script>
</head>

<html>
<body>

<img name="image01" src="9.jpg" width="200" height="128" border="0" value= "Show image">
<input type="button" onclick = "showImg()" >
</html>

</body>
</html>
Back to top
View user's profile Send private message
rangana
500+ Club


Joined: 27 Feb 2008
Posts: 614
Location: Cebu City Philippines

PostPosted: Wed Apr 23, 2008 5:27 am    Post subject: Re: Need help to display an image when button is clicked Reply with quote

First of all, you had a number of deprecated attributes Sad

Secondly, you are using XHTML...which means name is deprecated too Sad

Third, img has no value attribute Sad

This fix also includes your JS...See if it helps Wink

Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml1 transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Myimage</title>
<style type="text/css">
img{width:200px;height:128px;border:0;}
</style>
<script type = "text/javascript">

function showImg()
{   
var image = document.getElementById('image01');             
image.src='newImage.jpg'; //first image
}   
</script>
</head>

<html>
<body>

<img id="image01" src="9.jpg">
<br/>
<input type="button" onclick = "showImg()" value= "Show image">
</html>

</body>
</html>
Back to top
View user's profile Send private message Yahoo Messenger
Morta



Joined: 23 Apr 2008
Posts: 4

PostPosted: Wed Apr 23, 2008 11:01 am    Post subject: Re: Need help to display an image when button is clicked Reply with quote

Hi- thanks for the reply.
What does deprecated mean?

Your code has the same problem that mine does, that the image is displayed before the button is clicked.

There must be a way to define were the src of the image is in the JS portion of the code, because when it is defined in the HTML, it always shows.

Thanks-
M
Back to top
View user's profile Send private message
rangana
500+ Club


Joined: 27 Feb 2008
Posts: 614
Location: Cebu City Philippines

PostPosted: Wed Apr 23, 2008 11:09 am    Post subject: Re: Need help to display an image when button is clicked Reply with quote

...So you want the image to show just right after the button is pressed?..

If so, then this script will do it for you Wink
Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml1 transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Myimage</title>
<style type="text/css">
img{width:200px;height:128px;border:0;}
.show{display:block;}
.hide{display:none;}
</style>
<script type="text/javascript">
function showImg()
{
   var obj=document.getElementById('image01');
   obj.className = 'show';
}
</script>
</head>

<html>
<body>

<img id="image01" src="9.jpg" class="hide">
<br/>
<input type="button" onclick = "showImg()" value= "Show image">
</html>

</body>
</html>


...When I say deprecated, it means that you had been using attributes that is outdated and which doesn't conform witht the w3's standard Smile

...No worries, the script above does'nt have deprecated attributes Wink

See if it helps Smile
Back to top
View user's profile Send private message Yahoo Messenger
Morta



Joined: 23 Apr 2008
Posts: 4

PostPosted: Wed Apr 23, 2008 6:45 pm    Post subject: Now to do it in an array... Reply with quote

I've set up an image preload function.
I've also set up an image array, but am not sure that I did it right.

I don't know how to access the specific images, can you help me?
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml/DTD/xhtml1 transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>array2</title>

<style type="text/css">
img{width:200px;height:128px;border:0;}
</style>
<script type = "text/javascript">

function preloader() {           
imageObj = new Image();        // create object   
images = new Array(); {          // create array
images[i] = ["9.jpg" ,"10.jpg" ,"8.jpg" ,"7.jpg"]           // set image array   
}
// start preloading   
   for(var i=0; i<=3; i++)      {         
   imageObj.src=images[i];     
   }


function showImg()

var myimg = document.getElementById("image01").src
document.getElementById("image01").style.display="block";     


preloader()
</script>
</head>

<html>
<body>
<img id="image01" src="10.jpg" style="display:none" >
<br/>
<input type="button" onclick = "showImg()" value= "Show image">
</html>

</body>
</html>
Back to top
View user's profile Send private message
rangana
500+ Club


Joined: 27 Feb 2008
Posts: 614
Location: Cebu City Philippines

PostPosted: Thu Apr 24, 2008 1:35 am    Post subject: Re: Need help to display an image when button is clicked Reply with quote

So...what do you want to achieve (really)..I'm totally confused Sad

...You want three different image to show every onclick?....do you want random?...or just basing on the arrangement of the images in the array?
Back to top
View user's profile Send private message Yahoo Messenger
Morta



Joined: 23 Apr 2008
Posts: 4

PostPosted: Fri Apr 25, 2008 12:58 am    Post subject: Re: Need help to display an image when button is clicked Reply with quote

I got it.
Thanks for your help!! Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    DEVPPL Forum Index -> JavaScript Forum All times are GMT + 1 Hour
Page 1 of 1

 
 
Welcome to DEVPPL.com
You are not logged in, which means that you can't post in the forums.
Click here to Register

If you are a current member here on DEVPPL, please login below:

User: Pass:
Log me on automatically each visit:

 


Powered by phpBB © 2001, 2005 phpBB Group - Modified by DEVPPL

Flash Games - Sitemap