Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function myFunction()
  2.  {
  3.      // Page elements
  4.     var blokArray = document.querySelectorAll(".block");
  5.     var button = document.getElementById("btnAddBlock");
  6.     var divBlokken = document.getElementById("divBlokken");
  7.  
  8.     // Variables
  9.     var blokArrayLength = blokArray.length;
  10.  
  11.     // Change stage of .block elements when clicked
  12.     for( var i = 0 ; i < blokArrayLength ; i++ )
  13.     {
  14.         blokArray[i].addEventListener( "click", function()
  15.         {
  16.             if( this.classList.contains("stage1") )
  17.             {
  18.                 this.classList.remove("stage1");
  19.                 this.classList.add("stage2");
  20.             }
  21.             else if( this.classList.contains("stage2") )
  22.             {
  23.                 this.classList.remove("stage2");
  24.                 this.classList.add("stage3");
  25.             }
  26.             else if( this.classList.contains("stage3") )
  27.             {
  28.                 this.classList.remove("stage3");
  29.                 this.classList.add("stage1");
  30.             }
  31.             else
  32.             {
  33.  
  34.             }
  35.         });
  36.     }
  37.  
  38.     // Button functionality
  39.     button.addEventListener( "click", function()
  40.     {
  41.         // Create new row div
  42.         var newRowDiv = document.createElement("div");
  43.         newRowDiv.classList.add("row");
  44.         divBlokken.appendChild( newRowDiv );
  45.  
  46.         // Create new block div
  47.         var newBlockDiv = document.createElement("div");
  48.         newBlockDiv.classList.add("block");
  49.         newBlockDiv.classList.add("stage1");
  50.         newRowDiv.appendChild( newBlockDiv );
  51.  
  52.         // Add click event to new .block element
  53.         newBlockDiv.addEventListener( "click", function()
  54.         {
  55.             if( this.classList.contains("stage1") )
  56.             {
  57.                 this.classList.remove("stage1");
  58.                 this.classList.add("stage2");
  59.             }
  60.             else if( this.classList.contains("stage2") )
  61.             {
  62.                 this.classList.remove("stage2");
  63.                 this.classList.add("stage3");
  64.             }
  65.             else if( this.classList.contains("stage3") )
  66.             {
  67.                 this.classList.remove("stage3");
  68.                 this.classList.add("stage1");
  69.             }
  70.             else
  71.             {
  72.  
  73.             }
  74.         });
  75.     });
  76.  
  77.  }) ();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement