JS for popup after form succesful submit with delay 2 seconds and select first field and make focus

Hello everyone, I’m trying to write a js code to close a modal window after a successful form submission with a delay of 2 seconds.

I would also like, after opening the modal window, that the first input field would be immediately selected by assigning focus to it

here is the code i am using now

const openModalButtons = document.querySelectorAll(’[data-modal-target]’)

const closeModalButtons = document.querySelectorAll(’[data-close-button]’)

const overlay = document.getElementById(‘overlay’)

openModalButtons.forEach(button => {

button.addEventListener(‘click’, () => {

const modal = document.querySelector(button.dataset.modalTarget)

openModal(modal)

})

})

closeModalButtons.forEach(button => {

button.addEventListener(‘click’, () => {

const modal = button.closest('.modal')

closeModal(modal)

})

})

function openModal(modal) {

if (modal == null) return

modal.classList.add(‘active’)

overlay.classList.add(‘active’)

}

function closeModal(modal) {

if (modal == null) return

modal.classList.remove(‘active’)

overlay.classList.remove(‘active’)

}