Grow your CSS skills. Land your dream job.

Last updated on:

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. Dyllon
    Permalink to comment#

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

  2. Dyllon
    Permalink to comment#

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

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

Leave a Comment

Posting Code

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
    ``` 
    <div>
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
    </div>
    ```
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".