Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <script type="text/javascript">
- window.onload = function() {
- //This is a stupid assignment that does not make any sense...
- //Get the divs that are descendants of the placeholder div.
- var arrayOfTags = document.getElementById("placeholder").getElementsByTagName("div");
- //Push the values of all the divs in the arrayOfTags into a new array.
- options = [];
- for (var i = 0; i <= arrayOfTags.length - 1; i++) {
- options.push(arrayOfTags[i].childNodes[0].nodeValue);
- };
- //Now all the default options are in the options array.
- //Now to define the add and delete options.
- document.getElementById("addOption").onclick = function() {
- var newOption = prompt("What would you like to add?");
- options.push(newOption);
- var newOptionElement = document.createElement("div");
- var newOptionElementText = document.createTextNode(newOption);
- newOptionElement.appendChild(newOptionElementText);
- document.getElementById("placeholder").appendChild(newOptionElement);
- };
- document.getElementById("deleteOption").onclick = function() {
- var deleteOption = prompt("What would you like to delete?");
- var exists = false;
- for (var i = 0; i <= options.length - 1; i++) {
- if (deleteOption === options[i]) {
- document.getElementById("placeholder").removeChild(document.getElementById("placeholder").getElementsByTagName("div")[i]);
- options.splice(i, 1);
- i = options.length - 1;
- exists = true;
- };
- };
- if (exists === false) {
- alert("ERROR: Does not exist!");
- };
- };
- };
- </script>
- </head>
- <body>
- <div id="placeholder">
- <div>Steak</div>
- <div>Hot dogs</div>
- <div>Burgers</div>
- </div>
- <input type="button" id="addOption" value="Add option">
- <input type="button" id="deleteOption" value="Delete option">
- </body>
- </html>
Add Comment
Please, Sign In to add comment