7

What is the syntax for zindex?

I tried like this

document.getElementById('iframe_div1').style.zIndex =2000;
document.getElementById('iframe_div1').style.z-index =2000;

It is not working, it doesn't even show an error.

This is my snippet. It is working fine in HTML, but I want to set this CSS property in jQuery or JavaScript. The top/position attributes are not working...

z-index:1200px; top:450px; position:absolute; right:300px;

document.getElementById('iframe_div1').style.display='block';
$("#iframe_div1").css("z-index", "500");
$("#iframe_div1").css("right", "300");
$("#iframe_div1").css("top", "450");
document.getElementById('iframe_div1').style.position = "absolute";

This snippet is not as perfect as I expect. I want to move my div center of the page, but it just moving my div to the bottom of the page.

2
  • Why not put the css line in cssText for the element? Commented Aug 2, 2010 at 14:32
  • 2
    Mixing jQuery and plain JS (for the same task) is not so good. Commented Aug 2, 2010 at 16:51

5 Answers 5

15

document.getElementById('iframe_div1').style.zIndex = "2000"; is fine. Just make sure it's position is absolute or relative. like this,

document.getElementById('iframe_div1').style.position = "relative"; // could also be absolute
Sign up to request clarification or add additional context in comments.

Comments

9

The former should work all right. Use a DOM inspector like Firebug's "inspect element" to find out whether maybe the value gets assigned, but doesn't have the desired effect.

In jQuery, it would be

 $("#iframe_div1").css("z-index", "2000");

1 Comment

@Bharani what about position?
3

As this link mentions, zIndex only works if the element was positioned, so are you using something like position: absolute;?

http://www.w3schools.com/jsref/prop_style_zindex.asp

Comments

0

...but it just moving my div bottom of the page...

becouse:

document.getElementById('iframe_div1').style.display='block';

Must be:

document.getElementById('iframe_div1').style.display='relation';

//or

document.getElementById('iframe_div1').style.display='absolute';

1 Comment

No it is not. absolute is for position, not display. Please don't post wrong answer if you do not know much about the topic.
0
$('#iframe_div1').css({
        right : '300px',
        top : '450px',
        border : '1px solid red',
        color : '#f00'
});

Thanks Problem fixed

Comments

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.