Add tags to div using Javascript

Tags:



There are quite a few similar questions, but none are quite what I need nor can I fiddle them around to fit what I’m trying to do.

I have the following:

document.querySelector('#output').innerHTML = 'Hi ' + name; 

And would like to, somehow, add the <span> tags around name using the above. I’m attempting to create a simple chat AI for fun, and want to be able to hightlight certain words that the AI is saying.

Answer

Just add the spans in your string:

document.querySelector('#output').innerHTML = 'Hi <span>' + name + '</span>'; 


Source: stackoverflow