Guest User

Untitled

a guest
Jul 24th, 2018
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>Lab 6</title>
  4. </head>
  5. <body>
  6. <h1 id="headline">Hello...</h1>
  7. Contact list
  8. <ul class="-contact-list-view">
  9.  
  10. </ul>
  11. <script>
  12. var contactList = [];
  13.  
  14. //ingredient 1
  15. var protoContact = {
  16. first : "default first name",
  17. last : "default last name",
  18. email : "default email",
  19. toString : function() {
  20. return this.first + " " + this.last +
  21. ", " + this.email;
  22. }
  23. };
  24.  
  25. //ingredient 2
  26. var Contact = function() {};
  27.  
  28. //mixing:
  29. Contact.prototype = protoContact;
  30.  
  31. var build = function(firstName, lastName, emailAddr) {
  32. var newContact = new Contact();
  33.  
  34. newContact.first = firstName;
  35. newContact.last = lastName;
  36. newContact.email = emailAddr;
  37. contactList.push(newContact);
  38. };
  39.  
  40. var alternateBuild = function(props) {
  41. var newContact = Object.create(protoContact);
  42. for (k in props)
  43. if (props.hasOwnProperty(k))
  44. newContact[k] = props[k];
  45. contactList.push(newContact);
  46. };
  47.  
  48. /*
  49. alternateBuild({
  50. first : "Adam"
  51. });
  52. */
  53. var edit = function(contact) {
  54. var newEmail = prompt("Enter new email");
  55. if (newEmail)
  56. contact.email = newEmail;
  57. };
  58.  
  59. var find = function(lastname) {
  60. for (var i=0; i<contactList.length; i++)
  61. if (contactList[i].last == lastname)
  62. return contactList[i];
  63. };
  64.  
  65. var remove = function(contact) {
  66. for (var i=0; i<contactList.length; i++)
  67. if (contactList[i] == contact)
  68. contactList.splice(i, 1);
  69. };
  70.  
  71. build("Joe1", "Smith1", "joe1@gmail.com");
  72. build("Joe2", "Smith2", "joe2@gmail.com");
  73. build("Joe3", "Smith3", "joe3@gmail.com");
  74. build("Joe4", "Smith4", "joe4@gmail.com");
  75.  
  76. var list = document.getElementsByClassName(
  77. "-contact-list-view")[0];
  78.  
  79. renderContacts = function() {
  80. var listItems = "";
  81. for (var i=0; i<contactList.length; i++)
  82. listItems += "<li>" + contactList[i].toString()
  83. + "</li>";
  84. list.innerHTML = listItems;
  85. }
  86.  
  87. renderContacts();
  88. list.addEventListener("click", function(e){
  89. console.log(e);
  90. e.target.style.backgroundColor = "yellow";
  91. });
  92.  
  93. </script>
  94. </body>
  95. </html>
Add Comment
Please, Sign In to add comment