Attaching Events to Document Better Than Attaching Them to Elements?

While bouncing around StackOverflow, I’ve noticed a number of people attaching events (notably click events) to the document as opposed to the elements themselves.

Example:

Given this:

<button id="myButton">CLICK ME</button>

Instead of writing this (using jQuery just for brevity):

$('#myButton').on('click', function() { ... });

They do this:

$(document).on('click', function() { ... });

And then presumably use event.target to drill down to the element that was actually clicked.

Are there any gains/advantages in capturing events at the document level instead of at the element level?

There is one advantage: If #myButton isn’t always on the page (that is, it’s inserted through AJAX or altering innerHTML or the DOM directly), then there’s no need to recreate the event listener over and over.

That said, you shouldn’t do it due to both performance and obfuscation. If this is happening on your page, at least put the event on a containing element that is always there – #content or #menu or something similar.

And even then, to address the example code in your question, jQuery in particular has syntax to do that without manual checking:

$(document).on('click', '#myButton', function() { ... });

Although, as I said above, it is better to restrict the event to where it can appear:

$('#commentBox').on('click', '#myButton', function() { ... });

Maybe (and that’s a big maybe) in some (again, a big some) cases that could (and a big could) increase performance of the app, though I can’t say why.

What it definitely does is make the code harder to read, since you’d have to go into the callback to find out what element the event is really attaching too; and also potentially violates the DRY principle, since you can’t reuse the callback in another element event. You can certainly add another clause to your event.target if statement, but that would obfuscate the code even further.

Which brings me back to my first statement, somebody must have thought he could increase performance of the app, since the only reason for obfuscating code that makes sense, is improving performance. Sadly, most people tend to attack performance issues where they think the issue is, as opposed to profiling the code and determining exactly what is underperforming.

Again, I’m not sure that this would improve performance, but I’m quite sure it needlessly obfuscates your code, and that is in itself bad, especially a couple of months after you started writing it and come back to modify it. The code will likely now have multiple events bound to document with multiple ifs (some with one clause and some with n clauses) to check when trying to debug an event.

just my 2 cents

Edit: I really can’t come up with a situation where binding an event to the document would make it perform better. Perhaps the code you’ve seen requires capturing a click anywhere in the document and executing the same code regardless of where the click originates from? it’s the only reason I find could justify doing such a thing

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật