When trying to access a text node, you need to check if the nodeName
value of an Element is equal to "#text"
. In this scenario, I extract all the content within the DIV
with the class latest_posts
and navigate through its children by utilizing the childNodes
property following the selection of that element via a querySelector
.
Once I have retrieved the list of children
, I iterate over that list and only return those which are not of type #text
.
let latest_posts = document.querySelector('.latest_posts');
let newContent = latest_posts.cloneNode();
newContent.innerHTML = "";
Array.from(latest_posts.childNodes).forEach(child => {
if(child.nodeName != "#text") {
newContent.append(child);
}
});
latest_posts.innerHTML = newContent.innerHTML;
<div class="latest_posts style3 latest_posts--style2 latest_posts2 clearfix eluidf4c60403 latestposts2--dark element-scheme--dark">
<h3 class="m_title m_title_ext text-custom latest_posts2-elm-title" itemprop="headline"></h3>
VIEW ALL
<ul class="posts latest_posts2-posts">
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/law-for-bank/" itemprop="headline">Law For Bank</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/tech-blog/" itemprop="headline">Tech Blog</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/blog-about-tech-and-telcos/" itemprop="headline">Blog About Tech and Telcos</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
</ul>
<div>