Advertisement
CR7CR7

pickerChoice

Aug 16th, 2023
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const input = document.querySelector('.input')
  2. const word = document.querySelector('.word')
  3. const addbtn = document.querySelector('.add')
  4. const randombtn = document.querySelector('.random')
  5.  
  6. let inputValues = [];
  7.  
  8. addbtn.addEventListener('click', function() {
  9.   textInput = input.value
  10.   inputValues.push(textInput)
  11.   input.value = '';
  12.   displayInput();
  13. })
  14.  
  15. randombtn.addEventListener('click', function() {
  16.   if (inputValues.length > 0) {
  17.     const randomIndx = Math.floor(Math.random() * inputValues.length);
  18.     const random = inputValues[randomIndx]
  19.     const newEl = document.createElement('p')
  20.     newEl.textContent = random;
  21.     word.appendChild(newEl)
  22.   }
  23. })
  24.  
  25. let displayInput = () => {
  26.   word.innerHTML = inputValues.map(input => {
  27.     return `<p>${input} <button class="deletebtn">remove</button></p>`;
  28.   }).join('');
  29.  
  30.   // Add event listeners to the delete buttons
  31.   const deleteButtons = document.querySelectorAll('.deletebtn');
  32.   deleteButtons.forEach(button => {
  33.     button.addEventListener('click', function(e) {
  34.       e.target.parentElement.remove();
  35.       // Remove the deleted input from the array
  36.       const inputIndex = inputValues.indexOf(button.parentElement.textContent.trim());
  37.       if (inputIndex > -1) {
  38.         inputValues.splice(inputIndex, 1);
  39.       }
  40.     });
  41.   });
  42. }
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement