Home > Software engineering >  How to wrap <a> tags with <H1> tag?
How to wrap <a> tags with <H1> tag?

Time:11-06

I have the following:

<a class="clickable_element" href="https://www.mywebsite.com"> ==$0
   <div class="content">My Website</div>
</a>

I want to wrap all this in a "h1" tag.

How would I go about doing this using javascript?

CodePudding user response:

You would first have to create the h1 tag, append it to the DOM. Then append the tag to the tag. Like this

const aTag = document.querySelector('a')
const h1Tag = document.createElement('h1')

document.body.appendChild(h1Tag)
h1Tag.appendChild('aTag')

CodePudding user response:

I am not sure why you want to wrap this with h1 if you can just create a class for the div, but here's the code you can try:

HTML

<h1 id="wrap">
</h1>

JS

document.getElementById("wrap").innerHTML = "<a href='test.html'><div>My Website</div></a>"

  • Related