1

I have the first part of this problem down, I am able to select a random element from the array like so

function setImage()
{

var images = ['anemone.gif', 'ball.gif', 'crab.gif', 'fish2.gif', 'gull.gif',  'jellyfish.gif', 'moon.gif', 'sail.gif', 'shell.gif', 'snail.gif', 'sun.gif', 'sunnies.gif', 'whale.gif'];
var slots = [document.getElementById('slot0'), document.getElementById('slot1'), document.getElementById('slot2')];
document.getElementById('slot0').src = images[Math.floor(Math.random() * images.length)];
document.getElementById('slot1').src = images[Math.floor(Math.random() * images.length)];
document.getElementById('slot2').src = images[Math.floor(Math.random() * images.length)];
alert(images.indexOf(document.getElementById('slot2')));
}

However the second line is not giving me the correct index of the element, and I'm not sure how else to find it?

1
  • setting index in slot2 but alerting content of slot0 is it right ? Commented Oct 7, 2012 at 12:16

3 Answers 3

1

Is there a reason you're not just setting the random index into a variable and just using that?

function setImage()
{
    var images = ['anemone.gif', 'ball.gif', 'crab.gif', 'fish2.gif', 'gull.gif',  'jellyfish.gif', 'moon.gif', 'sail.gif', 'shell.gif', 'snail.gif', 'sun.gif','sunnies.gif', 'whale.gif'];
    var slots = [document.getElementById('slot0'), document.getElementById('slot1'),     document.getElementById('slot2')];

    var rnd0 = Math.floor(Math.random() * images.length);
    var rnd1 = Math.floor(Math.random() * images.length);
    var rnd2 = Math.floor(Math.random() * images.length);

    document.getElementById('slot0').src = images[rnd0];
    document.getElementById('slot1').src = images[rnd1];
    document.getElementById('slot2').src = images[rnd2];
    alert(rnd2);
}

If there is you should try alerting the source of the image and make sure that it matches in format to the images array.

If you're still having trouble, set up a jsfiddle with both html and JS so we can see what's going on.

Sign up to request clarification or add additional context in comments.

Comments

1

document.getElementById('slot2') will gove you the node (img in this case). So you can't find this in the array containing src values.

document.getElementById('slot2').src will give you full path (like http://....../file.ext), not just file name.

Use attributes property.

document.getElementById('slot2').attributes["src"]

Comments

0

Don't you mean:

alert(images.indexOf(document.getElementById('slot2').src));

4 Comments

there are two other slots (slot0, slot1) which also require the same treatment, the alert code gives me -1 as the index of the element from the array in all three instances.
You should probably post more code. What is in the array images? If there is NO image element in there with an ID attribute == 'slot0' your not going to find it :)
Try alert(images.indexOf(document.getElementById('slot2').src));
Well if you tried it earlier with the id set to 'slot0' it definitely fails :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.