Disable button after submit

Is this possible? I tried to do this via javascript in the header but this may it didn’t work

document.addEventListener('DOMContentLoaded', () => {
    const submitButton = document.querySelector('button[type="submit"]'); // Adjust the selector based on your button's specifics

    if (submitButton) {
        submitButton.addEventListener('click', () => {
            submitButton.disabled = true;
            submitButton.innerText = "Submitting..."; // Optional: Change button text
        });
    }
});