Applying a function to all buttons?

Im working on a quick script to apply an alert and redirect message to all anchors and buttons on a page. So far, I have this working for all anchors:

document.querySelector('body')
.addEventListener('click', function (event) {
    if
        (event.target.tagName === 'A' || event.target.tagName === 'button') { 
        event.preventDefault();
        alert("test");
       window.open('http://www.test.com', '_self');
    }
});

When i try to apply the same idea to all buttons (see after the || in the script), it does not apply. Am I missing something?

Answer

event.target.tagName results are capitalized.

A BUTTON TEXTAREA etc.

Leave a Reply

Your email address will not be published. Required fields are marked *