I would recommend avoiding the use of jQuery for this simple task, here's @Michael Coker example with vanilla JavaScript.
var banner = document.getElementById('banner');
if (banner) {
banner.appendChild(document.querySelector('.page h1'));
}
#banner h1 {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="banner">
<!---find if this div exists first--->
</div>
<div class="page">
<h1>Then find this and add it instead to above banner</h1>
</div>
Further explanation:
jQuery append() method performs several extra validations which you do not really need before using the appendChild
DOM method.
Your <h1>
element will always be of type 1
(Node.ELEMENT_NODE
), you can check the nodeTypes here.
Here's the method's jQuery Source borrowed from this answer:
append: function() {
return this.domManip(arguments, true, function( elem ) {
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
this.appendChild( elem );
}
});
}