Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Display <div> on top using zIndex and JavaScript increment

  1. #1
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Display <div> on top using zIndex and JavaScript increment

    I'm trying to figure out how I can call and element based on it's unique id and change the style to display:block and the z-index:+1. I've got the display:block working but I can't get the JavaScript to increment the z-index by one(+1) each time an element gets called. Here's what I've got so far...

    Code:
    <script type="text/javascript">
    var z=10;
    function ShowHide(id) {
      document.getElementById(id).style.display = "block";
      document.getElementById(id).style.zIndex = "z";
      "z++";
    
    }
    </script>

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    var z = 10;
    function ShowHide(id) {
      document.getElementById(id).style.display = "block";
      document.getElementById(id).style.zIndex = z++;
    }
    Or, to literally just increase the zIndex by one:
    Code:
    function ShowHide(id) {
      var k = document.getElementById(id).style;
      k.display = "block";
      k.zIndex = parseInt(k.zIndex) + 1;
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sweet!!! YOU DA MAN Work's great in Firefox, Safari, Mac IE 5.2

    Thanks...

  4. #4
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    One other thing...
    What is the best way to get that Script to return false so I don't have to put "return false" every time I call the script in my html?

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Add
    Code:
    return false;
    before the closing brace?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok, this isn't completely working. It shows it but then it dissapears real quick?
    When I put the "return false;" in the onClick it works...

    Code:
    <script type="text/javascript">
    var z = 10;
    function ShowHide(id) {
      var k = document.getElementById(id).style;
      k.display = "block";
      k.zIndex = parseInt(k.zIndex) + 1;
    return false;
    }
    </script>

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Remember that you have to return the function's return value:
    Code:
    onclick="return ShowHide('anID');"
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So, how do I do that inside the script and not in the onClick? Just putting "return false;}" didn't work?

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You have to explicitly return a value in the event handler, yes.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok, so I can't use "return false" in the <script> section.

    Since I'm pretty new to JavaScript, can you explain the differences / benefits of these two scripts

    #1
    Code:
    <script type="text/javascript">
    var z = 10;
    function ShowHide(id) {
      document.getElementById(id).style.display = "block";
      document.getElementById(id).style.zIndex = z++;
    }
    </script>

    #2
    Code:
    <script type="text/javascript">
    var z = 10;
    function ShowHide(id) {
      var k = document.getElementById(id).style;
      k.display = "block";
      k.zIndex = parseInt(k.zIndex) + 1;
    return false;
    }
    </script>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •