jquery

jQuery

Specific best practices and architectural patterns when working with jQuery.

Details

Language / Topic
javascriptJavaScript
Category
framework

Rules

balanced

jQuery

- Use event delegation (`$(parent).on('click', '.child', handler)`) over direct binding for dynamic elements. Cache jQuery selections in variables — never query the DOM repeatedly in loops. Use `.data()` for storing element-associated data.

jQuery

- Use event delegation (`$(parent).on('click', '.child', handler)`) over direct binding for dynamic elements. Cache jQuery selections in variables — never query the DOM repeatedly in loops. Use `.data()` for storing element-associated data.
- Chain methods (`$el.addClass('active').show().text('Done')`) to minimize DOM access. Use `$.ajax()` with promises (`.done()`, `.fail()`, `.always()`) over callback arguments. Prefer `.prop()` for boolean attributes (checked, disabled) and `.attr()` for HTML attributes. Minimize DOM manipulation — build HTML strings or use `DocumentFragment` then insert once.