Guest User

Untitled

a guest
Aug 3rd, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 3.62 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4.     <meta charset="utf-8" />
  5.     <title>DOM1 Example</title>
  6.  
  7.     <style>
  8.         #contacts {
  9.             width: 500px;
  10.             height: 500px;
  11.             border: 1px solid #0094ff;
  12.             background-color: #cfcfcf;
  13.             overflow-y: scroll;
  14.             overflow-x: hidden;
  15.         }
  16.         .card {
  17.             background-color: #35a4e5;
  18.             color: white;
  19.             font-size: x-large;
  20.             width: 450px;
  21.             padding: 10px;
  22.             margin: 5px;
  23.         }
  24.     </style>
  25.     <script src="Scripts/jquery-3.1.1.js"></script>
  26.  
  27.     <script>
  28.         // Handler ready event on window.document
  29.         var contacts = [
  30.             {
  31.                 id: 1,
  32.                 name: "Ram",
  33.                 phone: "453243243",
  34.                 email: "ram@abc.com"
  35.             },
  36.             {
  37.                 id: 2,
  38.                 name: "Shyam",
  39.                 phone: "453243222",
  40.                 email: "shyam@abc.com"
  41.             },
  42.             {
  43.                 name: "Ram",
  44.                 phone: "453243243",
  45.                 email: "ram@abc.com"
  46.             },
  47.             {
  48.                 name: "Shyam",
  49.                 phone: "453243222",
  50.                 email: "shyam@abc.com"
  51.             },
  52.             {
  53.                 name: "Ram",
  54.                 phone: "453243243",
  55.                 email: "ram@abc.com"
  56.             },
  57.             {
  58.                 name: "Shyam",
  59.                 phone: "453243222",
  60.                 email: "shyam@abc.com"
  61.             }
  62.         ];
  63.  
  64.         function getCard(item) {
  65.             return "<div class='card'> " +
  66.                 "Name  : " + item.name + "<br/>" +
  67.                 "Phone : " + item.phone + "<br/>" +
  68.                 "Email : " + item.email + "</div>";
  69.         }
  70.  
  71.         $(document).ready(function () {
  72.             // Initialize
  73.             $("#newcontact").hide();
  74.  
  75.             $("#shownew").on("click", function (e) {
  76.                 $("#contacts").hide();
  77.                 $("#newcontact").show();
  78.                 $(this).hide();
  79.             });
  80.  
  81.             $("#newcancel").on("click", function (e) {
  82.                 $("#contacts").show();
  83.                 $("#newcontact").hide();
  84.                 $("#shownew").show();
  85.             });
  86.  
  87.             $("#newadd").on("click", function (e) {
  88.                 if (window.confirm("Add new Contact?") == true) {
  89.  
  90.                     var newcontact = {
  91.                         name: $("#ncname").val(),
  92.                         phone: $("#ncphone").val(),
  93.                         email: $("#ncemail").val(),
  94.                     };
  95.  
  96.                     contacts.push(newcontact);
  97.                     $("#contacts").append(getCard(newcontact));
  98.  
  99.                     $("#contacts").show();
  100.                     $("#newcontact").hide();
  101.                     $("#shownew").show();
  102.                 }
  103.             });
  104.  
  105.             // Show Contacts
  106.             $.each(contacts, function (index, item) {
  107.                $("#contacts").append(getCard(item));
  108.             })
  109.         });
  110.  
  111.     </script>
  112. </head>
  113. <body>
  114.     <h1>Contacts</h1>
  115.  
  116.     <input id="shownew" type="button" value="New..." /> <br/>
  117.     <div id="contacts">
  118.     </div>
  119.  
  120.     <div id="newcontact">
  121.         <h2>New Contact</h2>
  122.         Name : <input type="text" id="ncname" /><br/>
  123.         Phone : <input type="text" id="ncphone" /><br />
  124.         Email : <input type="text" id="ncemail" /><br /><br/>
  125.         <input id="newadd" type="button" value="Add" />&nbsp;
  126.         <input id="newcancel" type="button" value="Cancel" />
  127.     </div>
  128. </body>
  129. </html>
Add Comment
Please, Sign In to add comment