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 />");


  1. Dyllon
    Permalink to comment#

    This will also work..

  2. Dyllon
    Permalink to comment#

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

  3. Jesse
    Permalink to comment#

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


    • Tim
      Permalink to comment#

      According to my CSS Pocket Reference book, “:first-of-type” is indeed a selector filter.

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.