Insert HTML father in pure javascript please

Insert HTML father in pure javascript please

Example is like this:

<div class="Australian-Brothers"></div>

It has to stay like this:

<a><div class="Australian-Brothers"></div></a>

how do i do it?

document.querySelector('.Australian-Brothers').before(document.createElement('a'));

One way to do this would be to create the <a> tag itself and then just append the div to it as a child…

// Create an anchor tag
let newAnchor = document.createElement('a'); 

// Append our div to it as a child
newAnchor.appendChild(document.querySelector('.Australian-Brothers')); 

// Append the newAnchor to where in the DOM you want it. Here we are just appending it back to the body of the document.
document.body.appendChild(newAnchor)

Again there is a few different ways to do this. But this example should give you some ideas of which is best for you.

Thanks for replying, is there any parent prototype?
example: appendFather

appendChild means “Move this node to be a child of the calling node.”
appendFather would mean “Move this node to be a parent of the calling node.”
“appendFather” = appendChild, but with the parameters reversed.

Though to refine the idea slightly, assuming that the idea isnt to tack a new node on the end, but instead supplant…

let newAnchor = document.createElement('a'); 
let current = document.querySelector(".Australian-Brothers");
current.insertAdjacentElement('beforebegin',newAnchor).appendChild(current);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.