Advertisement
-Annie-

01.Add/RemoveTown

Jul 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!-- JS Advanced Exam @ SoftUni - 13-Nov-2016 -->
  2.  
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6.     <meta charset="UTF-8">
  7.     <title>Add / Remove Towns</title>
  8.     <style>select, input { width: 100px }</style>
  9.     <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  10. </head>
  11.  
  12. <body onload="attachEvents()">
  13.  
  14. <div>Towns</div>
  15. <select id="towns" size="4">
  16.     <option>Sofia</option>
  17.     <option>Varna</option>
  18.     <option>Pleven</option>
  19. </select>
  20.  
  21. <button id="btnDelete">Delete</button>
  22.  
  23. <div>
  24.     <input type="text" id="newItem" />
  25.     <button id="btnAdd">Add</button>
  26. </div>
  27.  
  28. <script>
  29.     function attachEvents() {
  30.         let addButton = $('#btnAdd');
  31.         let deleteButton = $('#btnDelete');
  32.         let newItem = $('#newItem');
  33.         let townsList = $('#towns');
  34.  
  35.         addButton.on('click', function () {
  36.             if (newItem.val() !== ''){
  37.                 let newOption = $('<option>');
  38.                 newOption.text(newItem.val());
  39.                 townsList.append(newOption);
  40.                 newItem.val('');
  41.             }
  42.         })
  43.        
  44.         deleteButton.on('click', function () {
  45.             let townToDelete = $('option:selected');
  46.             townToDelete.remove();
  47.         })
  48.     }
  49. </script>
  50. </body>
  51. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement