Jelle De Loecker
Jelle De Loecker

Reputation: 21915

How can I add an element after another element?

I have a certain textbox and I want to add a div after it. I've tried the .append() function, but that only adds the div in the element.

For example, I have:

<input type="text" id="bla" />

and I want to change that into:

<input type="text" id="bla" /><div id="space"></div>

Upvotes: 218

Views: 358828

Answers (4)

Ashar Zafar
Ashar Zafar

Reputation: 402

Solved jQuery: Add element after another element

<script>
$( "p" ).after( "<strong>Hello</strong>" );
</script>

OR

<script type="text/javascript"> 
jQuery(document).ready(function(){
jQuery ( ".sidebar_cart" ) .insertAfter( "<a href='http://#'>Continue Shopping</a>" );
});
</script>

Upvotes: -5

Rowan
Rowan

Reputation: 5727

try using the after() method:

$('#bla').after('<div id="space"></div>');

Documentation

Upvotes: 370

Rifat
Rifat

Reputation: 7718

try

.insertAfter()

here

$(content).insertAfter('#bla');

Upvotes: 43

naivists
naivists

Reputation: 33501

First of all, input element shouldn't have a closing tag (from http://www.w3.org/TR/html401/interact/forms.html#edef-INPUT : End tag: forbidden ).

Second thing, you need the after(), not append() function.

Upvotes: 7

Related Questions