hoping to avoid repeatedly typing references to anchors by hand, I tried to come up with a way to make any occurrence of given terms into automagically linked references to a same-named anchor, like turn 'foo' into <a href="#foo">foo</a>
, 'bar' into <a href="#bar">bar</a>
, and so on.
I can't, however, seem to get my clunky approach to skip already linked occurrences (or other elements like style, script, etc). Positive selection (like
nodeList = document.querySelectorAll(".entry-content a");
) works just fine, but exclusion (like e.g.
nodeList = document.querySelectorAll(".entry-content:not(a, style, script)");
eludes me. I sifted thru quite a lot of questions that looked similar, already, but could adapt none for my stubborn problem :/ so, I must definitely be doing something wrong.
Your help figuring this out is much appreciated. Here's where I'm at right now:
function rep() {
const nodeList = document.querySelectorAll(".entry-content:not(a, style, script)");
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].innerHTML = nodeList[i].innerHTML.replace(/foo/g, '<a href="#foo" style="background: lime;">REPLACED FOO</a>');
}
}
But this just blatantly replaces every occurrence of 'foo' inside my class="entry-content", regardless of element type it appears in (instead of disregarding a and style and script elements).
Thank you for your look at this. Cheers -