,
,
, , - , , , , tags
answerHTML = cleanHtml(answerHTML);
// Option 2: Alternative - strip all HTML and use only text
// Uncomment the line below if you want to remove all HTML
// const answerText = answerEl.textContent.trim();
if (!answerHTML) return; // Skip if no answer content
// Add this Q&A pair to the schema
faqSchema.mainEntity.push({
"@type": "Question",
"name": questionText,
"acceptedAnswer": {
"@type": "Answer",
"text": answerHTML
}
});
});
// Helper function to clean HTML - keeps only basic formatting tags
function cleanHtml(html) {
// Create a temporary div element
const tempDiv = document.createElement('div');
tempDiv.innerHTML = html;
// Remove any script tags
const scripts = tempDiv.querySelectorAll('script');
scripts.forEach(script => script.remove());
// Convert divs to paragraphs
const divs = tempDiv.querySelectorAll('div:not([class]):not([id])');
divs.forEach(div => {
const p = document.createElement('p');
p.innerHTML = div.innerHTML;
div.parentNode.replaceChild(p, div);
});
// Get HTML with only basic formatting
return tempDiv.innerHTML;
}
// Only add the schema if we have FAQ items
if (faqSchema.mainEntity.length > 0) {
// Create the script element
const scriptElement = document.createElement('script');
scriptElement.type = 'application/ld+json';
scriptElement.textContent = JSON.stringify(faqSchema);
// Add it to the document head
document.head.appendChild(scriptElement);
// Optionally log for debugging
console.log("FAQ Schema added with", faqSchema.mainEntity.length, "items");
}
});