I've managed to get user inputs to store into localStorage
repeatedly into an array and now I need to display the input randomly on click. So basically someone just keeps clicking a generate button to see each input randomly and individually
<form id="add-to-wishlist">
<input type="text" name="wishlist-item" id="wishlist-item">
<button type="submit">Save</button>
<button onclick="window.localStorage.clear();">Clear All</button>
</form>
<button data-aos="zoom-in" data-aos-duration="2000" data-aos-delay="300" type="button" onclick="GetPlot()">Generate</button><br>
<textarea data-aos="flip-right" data-aos-duration="1500" data-aos-delay="250" name="plot" id ="plot" cols="50" rows="6"></textarea>
<p id="wishlist"></p>
<script>
// Get form, item, and wishlist
var addToWishList = document.querySelector('#add-to-wishlist');
var wishlistItem = document.querySelector('#wishlist-item');
var wishlist = document.querySelector('#wishlist');
addToWishList.addEventListener('submit', function (event) {
// Don't submit the form
event.preventDefault();
// Ignore it if the wishlist item is empty
if (wishlistItem.value.length < 1) return;
// Add item to wishlist
wishlist.innerHTML += ' ' + wishlistItem.value + ',';
// Clear input
wishlistItem.value = '';
// Save the list to localStorage
localStorage.setItem('wishlistItems', wishlist.innerHTML);
}, false);
// Check for saved wishlist items
var saved = localStorage.getItem('wishlistItems');
// If there are any saved items, update our list
if (saved) {
wishlist.innerHTML = saved;
}
function GetPlot(){
var randomIndex = Math.round(Math.random()*(wishlist.length-1));
var n = "";
n = n + wishlist[randomIndex];
wishlist.getElementById('plot').value = n;
}
</script>
Aucun commentaire:
Enregistrer un commentaire