Skip to content
Advertisement

Add tags to div using Javascript

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.

Advertisement

Answer

Just add the spans in your string:

document.querySelector('#output').innerHTML = 'Hi <span>' + name + '</span>'; 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement