I have one box div inside that, user can easily add link for h1 tag i.e for title tag. but I require that it should apply to the whole div. The anchor tag is coming from WordPress editor toolbar, so how can I extract that href
and add to whole div. code is using React.
eg :
<div class="box">
<h1>
<a href="https://stackoverflow.com/questions/13396721/if-href-url-change-add-class-to-parent- element">The new face of marketing</a>
</h1>
<p>Marketing</p>
</div>
I will need whatever user will add url for title it should apply to whole div and make div clickable with respect to its url.
3
Answers
IT IS NOT Working On this Snippet
You can see here Codepen link Link Open in New Tab Using Jquery
You can find the URL in a tag and set it to a div for clickability using "jQuery."
When you click on ".box," you’ll see the tag "a" inside. and click the link to open it in a new tab.
One approach to this is the following, with explanatory comments in the code:
JS Fiddle demo.
References:
Element.append()
.Element.classList
.Element.closest()
.Element.matches()
.Element.querySelector()
.Node.firstChild
.Node.insertBefore()
.Node.parentNode
.NodeList.prototype.forEach()
.If you’re looking for a jQuery solution as your tag indicates, this is how I would do it.
Explanation
Add a
href
tag to the div you want to be clickable as well as a classclickable-box
.Define a jQuery listener to watch for any click event on a div with the class
clickable-box
and redirect the page to the url defined in the div href tag.