I have created this code to insert adsense ads between questions loop, this code is working fine for me, Is there anyone know any other methods? to insert ads between loops, if know please let me know.
<script>
window.addEventListener('DOMContentLoaded', function() {
const posts = document.querySelectorAll('.qa-q-list-item');
const adCode = '<div class="ad-container">ad code goes here</div>';
let count = 0;
for (let i = 0; i < posts.length; i++) {
count++;
if (count === 2) {
const adContainer = document.createElement('div');
adContainer.innerHTML = adCode;
posts[i].parentNode.insertBefore(adContainer, posts[i].nextSibling);
count = 0;
}
}
});
</script>