findAndReplaceDOMText icon indicating copy to clipboard operation
findAndReplaceDOMText copied to clipboard

Creates a nested change

Open SimonPhilpott opened this issue 5 years ago • 0 comments

I've added function to include a href and target when finding and replacing with an anchor link;

The script trips over itself. if I try and find and replace a match within a second previously compiled match;

i.e. Some and Some Body

findAndReplaceDOMText(document.getElementById('newsPage'), { find: 'Some', wrap: 'a', wrapClass: 'ClassOne', wrapTarget: '_blank', wrapLink: 'http://www.linkOne.com' });

findAndReplaceDOMText(document.getElementById('newsPage'), { find: 'Some Body', wrap: 'a', wrapClass: 'ClassTwo', wrapTarget: '_blank', wrapLink: 'http://www.linkTwo.com' });

Outputs

<a class="ClassOne" href="http://www.LinkOne.com" target="_blank"><a class="ClassTwo" href="http://www.LinkTwo.com" target="_blank">Some</a></a>
<a class="ClassTwo" href="http://www.LinkTwo.com" target="_blank"> Body</a>

A more elegant solution would be be to have the replace on 'Some' not happen.

<a class="ClassOne" href="http://www.LinkOne.com" target="_blank">Some</a>
<a class="ClassTwo" href="http://www.LinkTwo.com" target="_blank"> Body</a>

SimonPhilpott avatar Oct 22 '20 14:10 SimonPhilpott