Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- .odd {
- background-color: blue;
- }
- .even {
- background-color: yellow;
- }
- </style>
- </head>
- <body>
- <ul id="listing">
- </ul>
- <button type="button" onclick="addElement()">Add element</button>
- <button type="button" onclick="removeElement()">Remove element</button>
- <script>
- //create a variable = 0 so
- var i = 0;
- function addElement(){
- var x = document.createElement("LI");
- // check if i is divisible by 2
- if(i % 2 == 0){
- // if it is, set the class to odd
- x.setAttribute("class", "odd");
- }else{
- // else set it to even
- x.setAttribute("class", "even");
- }
- x.innerHTML = "some text";
- document.getElementById("listing").appendChild(x);
- // add 1 to i
- i++;
- }
- function removeElement(){
- var y = document.getElementById("listing");
- y.removeChild(y.lastChild);
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement