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
  • 15,903
  • 28
  • 85
  • 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

60

Use after or insertAfter:

$('a').after('text');
$('text').insertAfter('a');
Jeff Sternal
  • 46,698
  • 7
  • 90
  • 119
  • 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
  • 179,909
  • 49
  • 229
  • 260
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
  • 6,784
  • 2
  • 37
  • 66
3

use .insertAfter() or .after()

Tim Cooper
  • 151,519
  • 37
  • 317
  • 271
jAndy
  • 223,102
  • 54
  • 301
  • 354
-1

You can do something like this:

$('').insertAfter('class name or content string') ;
Pang
  • 9,073
  • 146
  • 84
  • 117