Changed around line 1
+ document.addEventListener('DOMContentLoaded', function() {
+ const uploadBtn = document.getElementById('upload-btn');
+ const photoUpload = document.getElementById('photo-upload');
+ const galleryGrid = document.getElementById('gallery-grid');
+
+ // Load existing photos from localStorage
+ loadPhotos();
+
+ uploadBtn.addEventListener('click', function() {
+ photoUpload.click();
+ });
+
+ photoUpload.addEventListener('change', function(e) {
+ const files = e.target.files;
+ for (let i = 0; i < files.length; i++) {
+ const file = files[i];
+ if (file.type.startsWith('image/')) {
+ const reader = new FileReader();
+ reader.onload = function(e) {
+ const imgSrc = e.target.result;
+ savePhoto(imgSrc);
+ displayPhoto(imgSrc);
+ };
+ reader.readAsDataURL(file);
+ }
+ }
+ });
+
+ function savePhoto(imgSrc) {
+ let photos = JSON.parse(localStorage.getItem('photos') || '[]');
+ photos.push(imgSrc);
+ localStorage.setItem('photos', JSON.stringify(photos));
+ }
+
+ function loadPhotos() {
+ let photos = JSON.parse(localStorage.getItem('photos') || '[]');
+ photos.forEach(imgSrc => displayPhoto(imgSrc));
+ }
+
+ function displayPhoto(imgSrc) {
+ const galleryItem = document.createElement('div');
+ galleryItem.className = 'gallery-item';
+
+ const img = document.createElement('img');
+ img.src = imgSrc;
+ img.alt = 'Uploaded photo';
+
+ galleryItem.appendChild(img);
+ galleryGrid.appendChild(galleryItem);
+ }
+ });