subroutines

Discussion in 'Javascript' started by Matthew Snape, Feb 12, 2004.

  1. 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?
     
    Matthew Snape, Feb 12, 2004
    #1
    1. Advertising

  2. Matthew Snape

    Erwin Moller Guest

    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
     
    Erwin Moller, Feb 12, 2004
    #2
    1. Advertising

  3. Erwin Moller <> wrote in message news:<402b956f$0$561$4all.nl>...

    Thank you very much, that worked.

    > 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
     
    Matthew Snape, Feb 12, 2004
    #3
  4. Matthew Snape

    Erwin Moller Guest

    Matthew Snape wrote:

    > Thank you very much, that worked.



    Glad to help. :)
     
    Erwin Moller, Feb 13, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ReaprZero

    References and subroutines

    ReaprZero, Dec 4, 2003, in forum: Perl
    Replies:
    1
    Views:
    459
    Gunnar Hjalmarsson
    Dec 4, 2003
  2. MackS
    Replies:
    0
    Views:
    477
    MackS
    Mar 11, 2005
  3. tshad

    Global subroutines

    tshad, Jan 24, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    384
    IPGrunt
    Jan 26, 2005
  4. fd123456

    Global subroutines

    fd123456, Jan 25, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    419
    tshad
    Feb 4, 2005
  5. fd123456

    Global subroutines

    fd123456, Jan 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    406
    tshad
    Jan 28, 2005
Loading...

Share This Page