37

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

Qiao
  • 16,565
  • 29
  • 90
  • 117
  • Possible duplicate of [jQuery: Add element after another element](http://stackoverflow.com/questions/2244605/jquery-add-element-after-another-element) – Shafizadeh Dec 05 '15 at 22:50

5 Answers5

61

Use after or insertAfter:

$('a').after('text');
$('text').insertAfter('a');
Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
  • I try this: `$( "#hoa_maintenance_fee_value" ).after('select');` and it adds 12 new select dropdowns within the parent element. Is there a way to make it put just the one select box only after the target sibling? – TARKUS Jan 06 '17 at 14:53
6
$('a').after("text");
rahul
  • 184,426
  • 49
  • 232
  • 263
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>
KRyan
  • 7,308
  • 2
  • 40
  • 68
3

use .insertAfter() or .after()

jAndy
  • 231,737
  • 57
  • 305
  • 359
-1

You can do something like this:

$('').insertAfter('class name or content string') ;
Pang
  • 9,564
  • 146
  • 81
  • 122