Search code examples
javascripthtmldomtagsinnerhtml

How can I add html tags using .innerHTML?


I have an element that I want to update the innerhtml text and icon but my tag is being ignored.

 document.getElementById("nextBtn").innerHTML = "Next
 <i class="icon icon--right icon-arrow-right"></i>";
}

Solution

  • You need to escape the inner double quotes " in your string, or change your outer quotes to single quotes '; Examples:

    document.getElementById("nextBtn").innerHTML = "Next <i class=\"icon icon--right icon-arrow-right\"></i>";
    
    document.getElementById("nextBtn").innerHTML = 'Next <i class="icon icon--right icon-arrow-right"></i>';