The second element doesn’t display the expected result. Why and how to fix?
function process(sentence,element) {
const words = sentence.split(' ');
const container = document.getElementById(element);
let index = 0;
container.innerHTML = '';
for (let i = 0; i < words.length; i++) {
//console.log(words[i]);
container.innerHTML += words[i] + ' '; // Display the word
}
}
process("the quick <b>fox</b> jumps",'e1'); //displayed the word 'fox' as expected in bold
process("the quick <b> fox </b> jumps",'e2'); //displayed the word 'fox' as plain text, not as expected
div{
border:1px solid #999;
margin: 0 0 10px 0;
display:block;
font-size:22px
}
<div id="e1"></div>
<div id="e2"></div>