Applying a function to all buttons? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Applying a function to all buttons? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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.

We are here to answer your question about Applying a function to all buttons? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji