index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | //index.html <!doctype html> <html lang= "en" > <head> <meta charset= "utf-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1, shrink-to-fit=no" > <link rel= "stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.min.css" > <title></title> </head> <body> <div class = "container" > <div class = "row" > <div class = "col-12" > <h1>Dynamically Add & Remove Items From List Using JavaScript</h1> <hr> <div class = "input-group" > <input type= "text" class = "form-control" id= "candidate" required> <div class = "input-group-append" > <button onclick= "addItem()" class = "btn btn-add" type= "button" >Add Item</button> <button onclick= "removeItem()" class = "btn btn-remove" type= "button" >Remove Item</button> </div> </div> <ul id= "dynamic-list" > </ul> </div> </div> </div> <script> function addItem() { var ul = document.getElementById( "dynamic-list" ); var candidate = document.getElementById( "candidate" ); var li = document.createElement( "li" ); li.setAttribute( 'id' , candidate.value); li.appendChild(document.createTextNode(candidate.value)); ul.appendChild(li); } function removeItem() { var ul = document.getElementById( "dynamic-list" ); var candidate = document.getElementById( "candidate" ); var item = document.getElementById(candidate.value); ul.removeChild(item); } </script> <script src= "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/js/bootstrap.min.js" ></script> <style> body { font-family: 'Poppins' , sans-serif; background: rgb(107,186,222); background: -moz-linear-gradient(90deg, rgba(107,186,222,1) 0%, rgba(232,156,205,1) 100%); background: -webkit-linear-gradient(90deg, rgba(107,186,222,1) 0%, rgba(232,156,205,1) 100%); background: linear-gradient(90deg, rgba(107,186,222,1) 0%, rgba(232,156,205,1) 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr= "#d9d900" ,endColorstr= "#73ff96" ,GradientType=1); } h1{ color: rgb(49, 49, 49); margin-top: 40px; text-align: center; font-size: 32px; } .input-group{ margin: 50px 0px; } .input-group input{ height: 45px; } .btn-add{ background-color: rgb(12, 187, 50); color: white; padding: 0px 63px; } .btn-remove{ background-color: rgb(187, 12, 12); color: white; padding: 0px 50px; } ul{ display: flex; -ms-flex-direction: column; flex-direction: column; padding-left: 0; margin-bottom: 0; } li{ position: relative; display: block; padding: 10px 20px; margin-bottom: 10px; background-color: #fff; border: 1px solid rgba(0,0,0,.125); border-top-left-radius: .25rem; border-top-right-radius: .25rem; border-bottom-left-radius: .25rem; border-bottom-right-radius: .25rem; } </style> </body> </html> |