I have come across a code snippet that seems to be functioning well:
<html>
<head>
<title>Testing JS Highlighting</title>
<script type="text/javascript">
function highlight()
{
var t = document.getElementById('highlight').innerHTML;
t = t.replace(/(if|switch|for|while)\s*(\()/gi, "<b>$1</b>$2");
document.getElementById('highlight').innerHTML = t;
}
</script>
</head>
<body onload="javascript:highlight();">
<pre id="highlight">
1 if(foo) {
2 bar();
3 }
4
3 while(--baz) {
5 oof();
6 }
</pre>
</body>
</html>
Instead of targeting just one specific <pre>
tag with an ID, I am interested in applying this functionality to all the <pre>
tags on the page. It would be ideal to combine a specific tag with a unique identifier. Is there a way to enhance the existing JavaScript function to achieve this by maybe using
document.getElementsByTag(tag).getElementsById(id).innerHTML
or similar method in a loop? I attempted it myself but didn't get desired results. I am seeking a simple solution without anything too complex.
Looking forward to your suggestions.
--
nkd