JSFiddle - React, Tailwind, and code Playground
HTML
<div class="border-box clearfix">
<div class="blah">
<h4>h4 Title</h4>
<p>paragraph</p>
</div>
<div class="head clearfix">
<h4>h4 Title</h4>
<p>paragraph</p>
</div>
<p class="float-right">
<a href="#" onclick='getTitle(this)' class="btn-blue">anchor tag</a>
</p>
</div>
<div class="border-box clearfix">
<div class="blah">
<h4>h4 Title</h4>
<p>paragraph</p>
</div>
<div class="head clearfix">
<h4>h4 Title2</h4>
<p>paragraph</p>
</div>
<p class="float-right">
<a href="#" onclick='getTitle(this)' class="btn-blue">anchor tag</a>
</p>
</div>
JavaScript
Element.prototype.getParentByClassName = function(className) {
if(!this.parentNode)
return null;
else if(this.parentNode.hasClass(className))
return this.parentNode;
else
return this.parentNode.getParentByClassName(className);
};
Element.prototype.hasClass = function(className) {
return (this.classList || this.className.split(' ')).contains(className);
};
Element.prototype.text = function() {
return this.innerText || this.textContent;
};
if(!Array.prototype.contains)
{
Array.prototype.contains = function(item) {
for(var i = 0;i < item.length;++i)
if(this[i] == item)
return true;
return false;
}
}
window.getTitle = function(element) {
alert(element.getParentByClassName('border-box').querySelector('.head h4').text());
}