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

Comments

  1. User Avatar
    Dyllon
    Permalink to comment#

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

  2. User Avatar
    Dyllon
    Permalink to comment#

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

  3. User Avatar
    Jesse
    Permalink to comment#

    :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('');

    • User Avatar
      Tim
      Permalink to comment#

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

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-searchicon-staricon-tag