I've got the following simple JQuery that's embedded on each page of the project I'm playing around with:
$(document).ready(function(){
var replaced = $("body").html().replace(/\$([a-zA-Z]+)\b/g,
'<a href="https://finance.yahoo.com/quote/$1">\$1</a>');
$("body").html(replaced);
});
As you can see, if it finds a string of letters that are prefaced by $
, it assumes that a stock symbol was mentioned and generates a link to Yahoo Finance.
The issue is, as part of the process for generating content, a user creates their post, and then goes to a verification page which both renders their post, and has pre-populated fields with their input, so that they can make edits. This is where the replace falls apart, it generates HTML tags in input fields which cause them close prematurely, basically breaking everything!
I'm barely conversant with anything Javascript (my project is a Laravel 8 project, as I want to learn more about that, it just so happens that I need this part for the front end).
How would I go about having Jquery perform the text replacement, but leave input boxes alone?
Here is a paste of the page after its loaded and the script has run. You can see at page 25 that the input is getting broken due to the HTML code being replaced inside the input: