slevin37 slevin37 -4 years ago 94
jQuery Question

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 Source

event.target.tagName results are capitalized.

A BUTTON TEXTAREA etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download