SHARE
TWEET

[HTML] Add/Delete Items (oneFile)

LusienGG Jun 22nd, 2016 (edited) 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Add or Delete Items</title>
  6.     <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
  7.     <style>
  8.         li a {display: none}
  9.         li:hover a {display: inline}
  10.     </style>
  11. </head>
  12. <body>
  13. <h1>List of Items</h1>
  14. <ul id="items">
  15. </ul>
  16.  
  17. <input type="text" id="newItemText"/>
  18. <input type="button" value="Add" onclick="addItem()">
  19.  
  20. <script>
  21.     function addItem() {
  22.         let text = $('#newItemText').val()
  23.         addItemsToList(text)
  24.         console.log(text)
  25.         $("#newItemText").val('')
  26.     }
  27.  
  28.     function addItemsToList(text) {
  29.         let li = $('<li>')
  30.                 .append($("<span>").text(text))
  31.                 .append(" ")
  32.                 .append($("<a href='#' onclick=deleteItem(this)>[Delete]</a>"));
  33.         $('#items').append(li)
  34.     }
  35.  
  36.     function deleteItem(link){
  37.         $(link).parent().remove();
  38.     }
  39.  
  40.     $('#newItemText').keypress(function (e) {
  41.         if(e.keyCode == 13){
  42.                 $('#newItemText + input[type="button"]').click();
  43.         }
  44.     })
  45. </script>
  46. </body>
  47. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top