Computer >> Computer tutorials >  >> Programming >> Javascript

How to set the stack order of a positioned element in JavaScript?


To set the stack order, use the zIndex property. You can try to run the following code to implement zIndex property in JavaScript −

Example

<!DOCTYPE html>
<html>
   <head>
      <style>
         #myID {
            position: absolute;
            left: 20px;
            top: 20px;
            z-index: -1
         }
      </style>
   </head>
   <body>
      <img id = "myID"
         src="https://www.tutorialspoint.com/videotutorials/images/tutor_connect_home.jpg"
         width = "150" height = "150">
      <button type="button" onclick="myFunction()">Set Stack Order</button>
      <p>Z-index is -1</p>
      <script>
         function myFunction() {
            document.getElementById("myID").style.zIndex = "1";
         }
      </script>
   </body>
</html>