Skip to content
Advertisement

replace variable name with the respective variable value

I want to make a website in which variable name written in ‘{{‘ and ‘}}’ should get the value of variable. For example

JavaScript

the output should be hi its is computer

here is my code.

JavaScript

output of the above code is: hey it is computer. how are you. I am {{ data[1] }} years old. once visit this jsfiddle. Here is my code. https://jsfiddle.net/3ocmkwq2/

Thanks

Advertisement

Answer

This line

JavaScript

updates document.body.innerHTML, but does not update the doc variable, so this remains as it was before any replacements were made.

So next loop, doc is still the original, so the splice to get {{ }} re-gets the data[0].

To fix, update doc= after changing the .innerHTML

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement