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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help with img src as id

    So when the user clicks on a image in the cell of the table it is supposed to switch to another one of the images randomly. But i think i have some kind of syntax error in my x.src=newpic. Anyone see the problem? Here is the code:

    <html>
    <head>
    <title>Untitled</title>
    </head>
    <script language="JavaScript" type="text/javascript">
    <!--
    pics=new Array()
    pics[0]="picts1.gif"
    pics[1]="picts2.gif"
    pics[2]="picts3.gif"
    pics[3]="picts4.gif"
    pics[4]="picts5.gif"
    pics[5]="picts6.gif"

    function swappic(x){
    num=Math.floor(Math.random()*6)
    newpic=pics[num]
    x.src=newpic
    }

    //if(all sources are the same)
    //run function to slice image into table
    //-->
    </script>
    <body>
    <form>
    <table>
    <tr><td><img src="pict1.gif" id="a" onclick="swappic(this.id)"><td><img src="pict2.gif" id="b" onclick="swappic(this.id)"></tr>
    <tr><td><img src="pict3.gif" id="c" onclick="swappic(this.id)"><td><img src="pict4.gif" id="d" onclick="swappic(this.id)"></tr>
    </table>
    </form>
    </body>
    </html>

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Instead of this:
    Code:
    onclick="swappic(this.id)"
    Use this:
    Code:
    onclick="swappic(this)"
    Otherwise your script is essentially trying to set this.id.src (which doesn't make sense) rather than this.src (which makes sense and is what you need to have the script do).
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    it still comes up with broken image links. maybe its not passing the cell id to the function? x has to be the id not the src.

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    No, according to the function you have posted "x" needs to be an element (not an ID of an element, but the element itself - the HTML object which is the image element).

    Check your array of image sources and see if you have named them correctly. I see a difference between the images in the array and the images in the existing HTML. In the array the images are named "picts1.gif, picts2.gif,..." while in the regular HTML part of the page they are named "pict1.gif, pict2.gif,..."

    Does the "s" belong there in the image sources in array or not?
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    Zwake (09-13-2011)

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    wow your right haha. i cant believe i overlooked that. it works now, thank you for your help!


  •  

    Posting Permissions

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