Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2004
    Location
    Umea
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sending variables in onClick commands (?)

    I am a VBScript guy with a JavaScript problem. I have a
    <a href="pic/image_20.jpg" onclick="return showImage(this.href);">

    -tag that loads a large image with the help of:

    function showImage(sImage){
    document.getElementById('galleryimage').src=sImage;
    return false;
    }

    and

    <img src="x.gif" id="galleryimage">

    But I want a specifik text under each image too.

    How do I solve that? I add the text as a variable in each link? How? Please note, I have as good as NO knowledge of JScript.

    /Johan

  • #2
    New Coder
    Join Date
    Dec 2003
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you place the text in the alt property of the <img>?

    JS can access that.

  • #3
    New Coder
    Join Date
    May 2004
    Location
    Umea
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sounds like a possibility... But how do I do that? Could you give me an example?

    /johan

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Possibly one way would be like this


    <script>
    function showImage(sImage,txt){
    document.getElementById('galleryimage').src=sImage;
    document.getElementById('text').innerHTML=txt
    return false;
    }
    </script>


    <img src="x.gif" id="galleryimage">
    <div id="text"></div>

    <a href="pic/image_20.jpg" onclick="return showImage(this.href,'Hello World')">

  • #5
    New Coder
    Join Date
    May 2004
    Location
    Umea
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks!

    Great! Thanks alot!

    /Johan

  • #6
    New Coder
    Join Date
    Dec 2003
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I mis-read the problem the first time. This might work, depending on the number of images you want this to work with.


    Create an array to hold some objects you will create
    var my_objs = new Array();

    Create an Image object that loads the image off-screen for fast image swapping when the user clicks the link, and will hold all the info each image needs
    my_objs['monkey'] = new Image();
    my_objs['monkey'].src = 'ring_phone.gif';
    my_objs['monkey'].text = '432424';
    my_objs['monkey'].whatever = 'gdfvdfgdf';

    (You could use a number instead of a name)
    my_objs[0] = new Image();


    Change your link
    <a href="javascript:;" onclick="return showImage('monkey');">

    function showImage(name){
    document.getElementById('galleryimage').src=my_objs[name].src;
    return false;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •