Explain Codes LogoExplain Codes Logo

.append(), prepend(), .after() and .before()

javascript
dom-manipulation
performance-optimization
security-best-practices
Alex KataevbyAlex Kataev·Jan 29, 2025
TLDR

To insert content in the DOM using jQuery:

  • .append(): Places elements inside and at the end of the target:

    $('#target').append('<span>End</span>'); //Our element tags along 'at the end of the line'.
  • .prepend(): Inserts content at the start, inside the target:

    $('#target').prepend('<span>Start</span>'); //Shouting 'first dibs!', our element grabs the top spot!
  • .after(): Adds new content right after the target:

    $('#target').after('<span>Follow</span>'); //Our element says: 'Follow the leader!'
  • .before(): Positions elements just before the target:

    $('#target').before('<span>Before</span>'); //Our element cuts the line. Rude, but efficient!

Design your architecture, pick your tool, and place the content accordingly.

When to use which?

Add elements to parent (.append()/.prepend())

Creating a list or adding extra fields? Go for these:

  • .append(): Use when you need a new item at the end of the list.
  • .prepend(): When the new item should be first on the list.

Sibling rivalry (.after()/.before())

Adjusting layout or sequence?

  • .after(): Adds content after the targeted element.
  • .before(): Puts new content before the target.

Reversing the order (.appendTo()/.prependTo())

Opt for a more intuitive syntax:

  • .appendTo(): Inserts the selected element inside the target, at the end.
    $('<span>End</span>').appendTo('#target'); //New element asserts: 'End of the line, buddy!'
  • .prependTo(): Places the selected element at the beginning of the target.
    $('<span>Start</span>').prependTo('#target'); //It's as if the new element says: 'Move over, coming in hot!'

Pitfalls and best practices

Boost performance: Batch your changes

DOM manipulations can be expensive.

So, what to do?

Detach the parent, make the adjustments, then reattach the element. It's like pulling over to fix your car instead of trying to do so while driving.

Mind your security: Avoid XSS attacks

Inserting user input directly? Sanitize it first! Don't let their <script> tags run wild. In other words, remember your sanitizer!

Keep it unique: Selector specificity

Got multiple targets?

IDs should be as unique as fingerprints.

Remember: Class and tag selectors may hit more targets than intended. It's a bit like accidentally replying-all to an email; make sure your selectors specifically target what they need to.

Dive Deep: Practical usage of each function

Interactive content

Adding modals or alerts?

  • Pop in a modal at the end of the body using .append().
  • Push real-time alerts to the top of a list with .prepend().

Form field dynamism

Dynamically adding form fields enhances the user experience:

  • Add new options dynamically using .append().
  • A quick 'confirm email' field can directly follow an email input using .after().