DonC22
DonC22

Reputation: 21

How to wrap <a> tags with <H1> tag?

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?

Upvotes: 0

Views: 356

Answers (2)

MarkiePQ
MarkiePQ

Reputation: 1

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>"

Upvotes: 0

I-Am-Programming
I-Am-Programming

Reputation: 11

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')

Upvotes: 1

Related Questions