I have the following code:
<section class="section1">
//Section one content
</section>
<section class="section2">
<h4>Some Title</h4>
<input type="hidden" class="keys" value="1"/>
<div id="block-close-1"></div>
<div class="block" id="block-1">
//Block content
</div>
</section>
<section class="section3">
//Section3 content
</section>
What I want to do is take some html and insert it after "block-1"
The HTML I wish to add looks like this:
<input type="hidden" class="keys" value="2"/>
<div id="block-close-2"></div>
<div class="block" id="block-2">
//Block content
</div>
I have tried this:
var html = '//code as above';
$( "html" ).insertAfter( "#block-1");
The error I get is this:
Uncaught HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent.
I have also tried this: document.getElementById('#block-4').appendChild(html);
Error:
Uncaught TypeError: Cannot read property 'appendChild' of null
What is the correct way to add new HTML to the existing HTML?
htmlis a variable, so you have to use$(html).insertAfter( "#block-1");(without quotes (as @Elentriel answered)) or use code like this$("<div>some html...</div>").insertAfter( "#block-1");