View Full Version : Really having trouble here

11-20-2010, 05:39 AM
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?

11-20-2010, 06:02 AM
How is your grid made?
try blah = document.getElementByName(IMAGENAME).src

11-20-2010, 06:13 AM
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"');
var pos = "x" + xpos + "y" + ypos
eval('document.x' + xpos + 'y' + ypos + '.src="sprites/up.gif"');

Old Pedant
11-20-2010, 08:14 AM
He is asking, did you do <img name="x7y11"> or <img id="x7y11">????

For name=

var IMAGENAME = "x" + xpos + "y" + ypos
var imageSrc = document.images[IMAGENAME].src;
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:

var IMAGENAME = "x" + xpos + "y" + ypos
var imageSrc = document.getElementById(IMAGENAME).src;
document.getElementById(IMAGENAME).src = "something.gif";

11-20-2010, 07:10 PM
OK thanks for the help. So far so good. I do have one more question if you dont mind =)


^ 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!

Old Pedant
11-20-2010, 09:51 PM
You would need to then strip off the path. You can't change what HTML thinks the ".src" is.

Not hard:

var isrc = document.images[IMAGENAME].src
isrc = isrc.substring( isrc.lastIndexOf("/") + 1 );

That is, just get everything *past* the last / in the src.

11-22-2010, 01:42 AM
thank you sir!