I am echoing text from PHP to be loaded on my page through an AJAX call on my HTML page. I want Highlight.js to go through this text and highlight the syntax for me. However, it doesn't highlight the text. When I do this directly on the HTML page:
<pre>
<code id=resultDiv style="text-align:right;">
<p> int main() </p>
</code>
</pre>
it works. The int main() is highlighted. However,
echo
"int main()";
or even
echo "<p> int main() </p>"
to that same resultDiv from my PHP file to the page doesn't get highlighted.
Is there a fix for this? Why does this happen?
If I understood your question right, you're loading the content from a PHP page using Ajax.
When using highlight.js it will run through your html and highlight everything after the "load" event, however your Ajax call happens asynchronously after this event, which means the content is loaded after hightlight.js has done its pass.
highlight.js has the highlightBlock function that will trigger the code highlighting pass, in your case it will look like the following line of code
hljs.highlightBlock(document.getElementById("resultDiv"));
which you'll have to put after your content is added to the DOM