Guest User

Untitled

a guest
Jul 24th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>Lab 8</title>
  4. </head>
  5. <body>
  6. <h1 id="headline">Hello...</h1>
  7. Contact details<br/>
  8. First name: <span class="contact-first"></span><br/>
  9. Last name: <span class="contact-last"></span><br/>
  10. Email: <span class="contact-email"></span>
  11. <hr/>
  12. Last name: <input type="text" class="contact-last-input"/>
  13. <br/>
  14. <button class="find">Find contact</button>
  15. <script>
  16. var contactList = [];
  17.  
  18. //ingredient 1
  19. var protoContact = {
  20. type : "contact",
  21. first : "default first name",
  22. last : "default last name",
  23. email : "default email",
  24. toString : function() {
  25. return this.first + " " + this.last +
  26. ", " + this.email;
  27. }
  28. };
  29.  
  30. //ingredient 2
  31. var Contact = function() {};
  32.  
  33. //mixing:
  34. Contact.prototype = protoContact;
  35.  
  36. var build = function(firstName, lastName, emailAddr) {
  37. var newContact = new Contact();
  38.  
  39. newContact.first = firstName;
  40. newContact.last = lastName;
  41. newContact.email = emailAddr;
  42. contactList.push(newContact);
  43. };
  44.  
  45. var alternateBuild = function(props) {
  46. var newContact = Object.create(protoContact);
  47. for (k in props)
  48. if (props.hasOwnProperty(k))
  49. newContact[k] = props[k];
  50. contactList.push(newContact);
  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. document.getElementById("headline").innerHTML="Contacts";
  77.  
  78. document.getElementsByClassName("find")[0].addEventListener(
  79. "click", function(e) {
  80. var lastname = document.getElementsByClassName
  81. ("contact-last-input")[0].value;
  82. var contact = find(lastname);
  83. render(contact);
  84. }
  85. );
  86.  
  87. var render = function(contact) {
  88. document.getElementsByClassName("contact-first")[0]
  89. .innerHTML = contact.first;
  90. document.getElementsByClassName("contact-last")[0]
  91. .innerHTML = contact.last;
  92. document.getElementsByClassName("contact-email")[0]
  93. .innerHTML = contact.email;
  94. }
  95.  
  96. </script>
  97. </body>
  98. </html>
Add Comment
Please, Sign In to add comment