Connecting Tech Pros Worldwide Help | Site Map

z-index, layers, onclick and getElementById

Newbie
 
Join Date: Sep 2009
Posts: 3
#1: Sep 15 '09
Friends,

Thanks for being here! I am pretty new to all this ...

I want to avoid people having to scroll on my website to see the content.

Therefore I am using 4 layers overlapping each other so you can see all "pages".

I am trying to use onclick to change the z-index value of the clicked layer to be the highest so it moves to the top and can be read (and setting the other pages z-index to lower values of course).

Here is an example:
<div id="layer1" onclick"document.getElementById('layer1').style.zi ndex='20'" </div>.

Nothing happens when I click - or at least not what I expected ...:)

I think I am doing something wrong in <script> but I don't get it.

Anyone with a good example or a better way to do it?

Grateful for your suggestions!

Best wishes
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,126
#2: Sep 15 '09

re: z-index, layers, onclick and getElementById


in case the divs are fully overlapping then the div that lays 'above' all others intercept the event ... you should show an example or explain a bit more what you would expect the clicks to do ...

kind regards
Newbie
 
Join Date: Sep 2009
Posts: 3
#3: Sep 16 '09

re: z-index, layers, onclick and getElementById


Thanks for replying - I really appreciate it!

Here is the whole example I found in a book:

Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <STYLE type="Text/css">
  4.     #layer1 {
  5.     position: absolute;
  6.     top:    50px;
  7.     left:    50px;
  8.     width: 200px;
  9.     height: 100px;
  10.     z-index: 0;
  11.     background-color:red}
  12.     #layer2 {
  13.     position: absolute;
  14.     top:    75px;
  15.     left:    90px;
  16.     width: 150px;
  17.     height: 100px;
  18.     z-index: 10;
  19.     background-color:yellow}
  20. </STYLE>
  21. </HEAD>
  22. <BODY>
  23.     <div id="layer1" onClick="document.getElementById('layer1').style.zindex='20'"></DIV>
  24.     <div id="layer2" onClick="document.getElementById('layer1').style.zindex='0'"></DIV>
  25. </BODY>
  26. </HTML>
It displays the two layers correctly as such.

What I expected to happen is that the red layer would come onto the top when I click on it and vice versa.

This is of course just an example. I would have text within the layers for the visitor to read so the layers would naturally be larger.

The idea is to avoid having to scroll at all.

I would also like to use more than two layers - at least five.

However, it does not seem to work as explained in the book ...:) I have checked it a dozen of times to make sure I copied it correctly but but ...who knows ...

I hope this clarifies it! Please let me know if more info is needed.

Best wishes
Rolf
Newbie
 
Join Date: Sep 2009
Posts: 3
#4: Sep 16 '09

re: z-index, layers, onclick and getElementById


I am very sorry that I did not use [code] - I am new here and I missed that. I will look for an example.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: Sep 16 '09

re: z-index, layers, onclick and getElementById


Two quick points:
1. It's zIndex, not zindex (notice the capital "i"). The dash is converted to camel-case, so z-index becomes zIndex.
2. z-indexes are numbers.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#6: Sep 16 '09

re: z-index, layers, onclick and getElementById


Just to clarify acoder's second point...the numbers should not have quotes around them (they should not be '2') instead they should just be numbers.
Reply

Tags
getelementbyid, onclick, zindex