WK_of_Angmar

Yahoo! Answers - Tom's Question

Oct 30th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.76 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. window.onload = function() {
  5.     //This is a stupid assignment that does not make any sense...
  6.     //Get the divs that are descendants of the placeholder div.
  7.     var arrayOfTags = document.getElementById("placeholder").getElementsByTagName("div");
  8.     //Push the values of all the divs in the arrayOfTags into a new array.
  9.     options = [];
  10.     for (var i = 0; i <= arrayOfTags.length - 1; i++) {
  11.         options.push(arrayOfTags[i].childNodes[0].nodeValue);
  12.     };
  13.     //Now all the default options are in the options array.
  14.    
  15.     //Now to define the add and delete options.
  16.     document.getElementById("addOption").onclick = function() {
  17.         var newOption = prompt("What would you like to add?");
  18.         options.push(newOption);
  19.         var newOptionElement = document.createElement("div");
  20.         var newOptionElementText = document.createTextNode(newOption);
  21.         newOptionElement.appendChild(newOptionElementText);
  22.         document.getElementById("placeholder").appendChild(newOptionElement);
  23.     };
  24.     document.getElementById("deleteOption").onclick = function() {
  25.         var deleteOption = prompt("What would you like to delete?");
  26.         var exists = false;
  27.         for (var i = 0; i <= options.length - 1; i++) {
  28.             if (deleteOption === options[i]) {
  29.                 document.getElementById("placeholder").removeChild(document.getElementById("placeholder").getElementsByTagName("div")[i]);
  30.                 options.splice(i, 1);
  31.                 i = options.length - 1;
  32.                 exists = true;
  33.             };
  34.         };
  35.         if (exists === false) {
  36.             alert("ERROR: Does not exist!");
  37.         };
  38.     };
  39. };
  40. </script>
  41. </head>
  42. <body>
  43. <div id="placeholder">
  44.     <div>Steak</div>
  45.     <div>Hot dogs</div>
  46.     <div>Burgers</div>
  47. </div>
  48. <input type="button" id="addOption" value="Add option">
  49. <input type="button" id="deleteOption" value="Delete option">
  50. </body>
  51. </html>
Add Comment
Please, Sign In to add comment