I am new to JS and would like to add words to a sentence or replace words, specfically like this:
HTML file:
<body>
<p id="paragraph">
My name is <first> <last>. I have been at ... for <years> and plan to <goals> after completing my courses. <p>
<script src="script.js">
var paragraph = document.getElementById("paragraph");
paragraph.replace("first", "firstName");
paragraph.replace("last", "lastName");
paragraph.replace("years", "yearsOfStudy ");
paragraph.replace("goals", "goal");
</script>
<body>
JS file:
let firstName = "Max";
let lastName = "Jones";
let yearsOfStudy = "about a year";
let goal = "getting a job as a software developer";
This isnt working as intended but I dont know why. Please help me figure this out, it doesnt matter if its using the replace method or another method as long as the varibales declared in the JS file are being used and an inline script is used to insert those words into the text.
srcand content, yourreplacecalls are looking for strings that don't exist, yourgetElementByIdreturns an element (not the text in the element), the second argument in your replace calls is a string (not a variable reference), and the variables in your JS file won't be available the way you're expecting. I suggest reading up on each of those topics (innerText/innerHtml, script tags,string.replace, and scope) and seeing what fixes you're able to implement.My name is <first> <last>. I have been [...]and you want to dynamically replace them with your variables? If that's so, make sure not to forget to escape them (use<and>instead of<and>, respectively.);