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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Really having trouble here

    OK i really hope someone can help me because this has put a complete halt to what I'm doing right now.

    i have various images in a 12x12 grid with names such as x1y1, x2y2, ect

    I need to check what x1y1's image SRC is. I have been trying stuff like this:

    var xpos = 1
    var ypos = 1
    var IMAGENAME = "x" + xpos + "y" + ypos

    blah = ('document.' + IMAGENAME + '.src')

    if (blah == "something.gif") {
    do what i want
    }


    Just doesn't work, nor has the 80 other things I've tried lol.

    Can anyone help me out?
    Last edited by es696; 11-20-2010 at 05:56 AM.

  • #2
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    How is your grid made?
    try blah = document.getElementByName(IMAGENAME).src

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    the grid is just tons of images with x and y names. no IDs

    k i have this, with your idea included:

    var xpos = 1
    var ypos = 1

    function moveup() {
    var test1 = "x" + xpos + "y" + ypos
    document.form.textbox1.value = document.getElementByName(test1).src


    the two lines above^ dont work and code below (dont pay attention to it) fails to execute.:


    if(xpos == 10) {
    } else {
    eval('document.x' + xpos + 'y' + ypos + '.src="tiles/grass.jpg"');
    xpos=(xpos+1)
    var pos = "x" + xpos + "y" + ypos
    eval('document.x' + xpos + 'y' + ypos + '.src="sprites/up.gif"');
    }}
    Last edited by es696; 11-20-2010 at 06:25 AM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    He is asking, did you do <img name="x7y11"> or <img id="x7y11">????

    For name=
    Code:
    var IMAGENAME = "x" + xpos + "y" + ypos
    var imageSrc = document.images[IMAGENAME].src;
    or
    document.images[IMAGENAME].src = "something.gif";
    But that's a bit old fashioned.

    Would be better to use id's instead of names, and then:
    Code:
    var IMAGENAME = "x" + xpos + "y" + ypos
    var imageSrc = document.getElementById(IMAGENAME).src;
    or
    document.getElementById(IMAGENAME).src = "something.gif";
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    OK thanks for the help. So far so good. I do have one more question if you dont mind =)

    document.images[IMAGENAME].src

    ^ That worked but it gives me the full path to the SRC. Is there a way to just return the value alone?

    Thanks for the help guys i really appreciate it!

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    You would need to then strip off the path. You can't change what HTML thinks the ".src" is.

    Not hard:
    Code:
    var isrc = document.images[IMAGENAME].src
    isrc = isrc.substring( isrc.lastIndexOf("/") + 1 );
    That is, just get everything *past* the last / in the src.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    es696 (11-22-2010)

  • #7
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you sir!


  •  

    Posting Permissions

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