Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4. <head>
  5. <style>
  6. .odd {
  7. background-color: blue;
  8. }
  9.  
  10. .even {
  11. background-color: yellow;
  12. }
  13.  
  14. </style>
  15.  
  16. </head>
  17.  
  18. <body>
  19.  
  20. <ul id="listing">
  21.  
  22. </ul>
  23.  
  24. <button type="button" onclick="addElement()">Add element</button>
  25. <button type="button" onclick="removeElement()">Remove element</button>
  26.  
  27. <script>
  28.  
  29. //create a variable = 0 so
  30. var i = 0;
  31.  
  32. function addElement(){
  33. var x = document.createElement("LI");
  34.  
  35. // check if i is divisible by 2
  36. if(i % 2 == 0){
  37. // if it is, set the class to odd
  38. x.setAttribute("class", "odd");
  39. }else{
  40. // else set it to even
  41. x.setAttribute("class", "even");
  42. }
  43. x.innerHTML = "some text";
  44. document.getElementById("listing").appendChild(x);
  45. // add 1 to i
  46. i++;
  47. }
  48.  
  49. function removeElement(){
  50. var y = document.getElementById("listing");
  51. y.removeChild(y.lastChild);
  52. }
  53.  
  54. </script>
  55.  
  56. </body>
  57.  
  58. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement