subroutines

M

Matthew Snape

hi, I have recently tried to make a web page where there is a number
of pictures each named with a number (1,2,3,4,5 etc). I then have
links on the page which are supposed to change the picture. I know
how to do this with the onclick event. However, I want to run several
other things when the link is clicked. Therefore I put a subroutine
that accepts the pictures name in the subroutine like this:
onclick="top.dostuff(4)" This executed the sub ok. However I also
wanted to change the picture so I tried to do it like this:

function dostuff(picnumber) {
var picture = new Image;
picture.src ="alternativepic.gif";
document.picnumber.src = picture;
}

with the link like this:

<img src="a.gif" name="1"> <a href="#" onclick="dostuff(1)"> 1 </a>
<img src="a.gif" name="2"> <a href="#" onclick="dostuff(1)"> 2 </a>
<img src="a.gif" name="3"> <a href="#" onclick="dostuff(1)"> 3 </a>



As you can see I want to be able to change the specific picture
depending on the value of picnumber. Any ideas?
 
E

Erwin Moller

Hi,

I am not sure if you can name your picture 1, 2, 3
I think that a name should start with a letter or underscore.

Try this:

function dostuff(picnumber) {
document.images["pic"+picnumber].src="somethingelse.pic";
}

with the link like this:

<img src="a.gif" name="pic1"> <a href="#" onclick="dostuff(1)"> 1 </a>
<img src="a.gif" name="pic2"> <a href="#" onclick="dostuff(2)"> 2 </a>
<img src="a.gif" name="pic3"> <a href="#" onclick="dostuff(3)"> 3 </a>


Good luck,
Erwin Moller
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top