document.addEventListener('DOMContentLoaded', function () { console.log('πŸ“š Ebook Button Handler Loaded'); document.querySelectorAll('.open-book-popup').forEach(function (btn, i) { btn.addEventListener('click', function (e) { e.preventDefault(); console.log(`🟩 Clicked Ebook Button #${i + 1}`, btn); const container = btn.closest('.elementor-container, .elementor-widget'); if (!container) return console.warn('❌ No container found for this button.'); const bookEl = container.querySelector('[data-book]'); const popupEl = container.querySelector('[data-popup-id]'); const bookTitle = bookEl?.getAttribute('data-book') || ''; const popupId = popupEl?.getAttribute('data-popup-id') || ''; const input = document.querySelector('input[name="book_title"]'); console.log('πŸ“– Book Title:', bookTitle); console.log('πŸ†” Popup ID:', popupId); console.log('πŸ“© Hidden Input:', input); if (input && bookTitle) { input.value = bookTitle; console.log('βœ… Set hidden field to:', bookTitle); } else { console.warn('⚠️ Missing input or book title'); } if (typeof JetPopup !== 'undefined' && popupId) { console.log('πŸš€ Triggering JetPopup ID:', popupId); JetPopup.showPopup(parseInt(popupId)); } else { console.warn('❌ JetPopup not available or popup ID missing'); } }); }); });