AFW
AFW

Reputation: 3

setting z-index in javascript: null error

I have a series of images laid on top of one another. I intend to bring one to the front and animate its opacity. I am using the following code:

document.getElementById('newFrame').style.zIndex="10";

Having set the id in the following way:

var newFrame = "p" + rand;

(rand is a random number, and the image id's are p1, p2 etc)

But I'm getting the error "Cannot read property 'style' of 'null'"

and despite multiple changes I can not get this to work - please help.

Upvotes: 0

Views: 282

Answers (2)

Srdjan Pejic
Srdjan Pejic

Reputation: 8202

Very simply, you aren't using the variable you've set, you're using a string instead.

Your code is looking for an element with the ID of 'newFrame' rather than an ID 'p'+rand()

Upvotes: 0

V31
V31

Reputation: 7666

newFrame is an object and so you need to reference it like one.You need to do something like this:

document.getElementById(newFrame).style.zIndex="10";

Upvotes: 1

Related Questions