Forum Moderators: open
<button class="button" onClick="console.log('click worked')">Do This</button> $('.button')
.mouseover(function() {
console.log('moused over');
})
.click(function(e) {
console.log('made it to .button click');
}); var tester = 'made it to footer.js'; if (tester)
console.log(tester + ', made it to internal.js'); console.log(typeof requiredFields);
$('.button')
.mouseover(function() {
console.log('moused over');
})
.click(function(e) {
console.log('made it to .button click');
});
$(document)
.on('mouseover', '.button', function(e) {
console.log('moused over');
}) function buttonClick(e) {
console.log('made it to .button click');
}
$('.button').click(function(e) { buttonClick(e); }); $('.button').click(function(e) { buttonClick(e); }); $(document)
.on('click', '.button', e => { ... })
.on('click', '#foo', () => { ... })
.on('click', '#bar', () => { ... })
.on('mouseover', '#bar', () => { ... })
.on('mouseout', '#bar', () => { ... });
Attaching many delegated event handlers near the top of the document tree can degrade performance. Each time the event occurs, jQuery must compare all selectors of all attached events of that type to every element in the path from the event target up to the top of the document. For best performance, attach delegated events at a document location as close as possible to the target elements.
$('form, #emptyElement').on('click', '.button', e => { ... }); presumably because the second form hasn't been added to the DOM yet.
Is there value to adding return false; to each of these functions? I understand from the docs that doing so would trigger both event.stopPropagation() and event.preventDefault().
I think you answered your own question there :-)
$(document).on('mouseover', '.button', () => { console.log('moused over') }); $(document).on('mouseover', '.button', () => {
console.log('moused over');
return false;
});
// I also tried this for testing
$(document).on('mouseover', '.button', e => {
console.log('moused over');
if (e.stopPropagation) e.stopPropagation();
e.cancelBubble = true;
if (e.preventDefault) e.preventDefault();
else e.returnValue = false;
return false;
});