How can I add an element after another element?

HtmlJqueryAppendInsertafter

Html Problem Overview


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>

Html Solutions


Solution 1 - Html

try using the after() method:

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

Documentation

Solution 2 - Html

try

.insertAfter()

here

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

Solution 3 - Html

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.

Solution 4 - Html

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionskeritView Question on Stackoverflow
Solution 1 - HtmlRowanView Answer on Stackoverflow
Solution 2 - HtmlRifatView Answer on Stackoverflow
Solution 3 - HtmlnaivistsView Answer on Stackoverflow
Solution 4 - HtmlAshar ZafarView Answer on Stackoverflow