A Web Design Community curated by Chris Coyier

A little dab'll do ya

Code Snippets

Home » Code Snippets » jQuery » Insert Element Between Other Elements Submit one!

Insert Element Between Other Elements

For example, if you have five paragraphs in a row and you need to insert a break tag in between all of them. You can't put it before all five (you don't need one at the top) or after all five (you don't need one at the bottom), you only need four.

$("p:not(:last-of-type)").after("<br />");

Subscribe to The Thread

  1. Dyllon says:

    This will also work..
    $("p:not(:first-of-type)").before("");

  2. Dyllon says:

    there’s supposed to be a break tag between those quotes.

  3. Jesse says:

    :first-of-type is not a selector filter so the sinppent doesn’t work.
    Here is the correction


    $('.parent_of_target_p_tags').children('p.not(:last-child)').after('');

It's Your Turn

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
--- The Management ---