---
title: jQuery Best Practices
type: agent/instruction
tags: [jquery, legacy]
---

{{h1}}jQuery

{{h2}}Modern Context
- Primarily for legacy maintenance
- Consider vanilla JS or modern frameworks

{{h2}}Selectors
- Cache jQuery objects
- Use specific selectors
- Use `find()` on cached objects

{{h2}}Performance
- Batch DOM updates
- Use event delegation
- Chain methods

{{h2}}Events
- Use `.on()` for binding
- Event delegation for dynamic content
- Unbind when removing elements

{{h2}}AJAX
- Use promises (`.done()`, `.fail()`)
- Handle errors
- Consider `fetch` API instead

{{h2}}DOM
- Use `.text()` for user content (XSS safety)
- Build HTML outside loops
- Detach before heavy manipulation

{{h2}}Avoid
- Don't query DOM repeatedly
- Don't use deprecated methods (`.live()`, `.bind()`)
- Don't ignore memory leaks
