Alex Rivera | Logout

jQuery add next to (add after)

Asked 2010-04-09T13:01:28.370
38

HTML is
<a>ref</a>

I need to get
<a>ref</a>text

How can i do this? $('a').append('text') only insert text into <a></a>, not after it

Edit
Report

1 Answer

4

Using the following HTML:

<div class="container">
  <h2>Greetings</h2>
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>

Content can be created and then inserted after several elements at once:

$('.inner').after('<p>Test</p>');

Each inner element gets this new content:

<div class="container">
  <h2>Greetings</h2>
  <div class="inner">Hello</div>
  <p>Test</p>
  <div class="inner">Goodbye</div>
  <p>Test</p>
</div>

An element in the DOM can also be selected and inserted after another element:

$('.container').after($('h2'));

If an element selected this way is inserted elsewhere, it will be moved rather than cloned:

<div class="container">
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>
<h2>Greetings</h2>
answered 2012-10-11T16:15:45.783

Your Answer