Supposing that the element e-mailError
is a visible component in your HTML document (such as a span, paragraph, or division), you can load the icon and corresponding text by manipulating the .innerHTML
attribute of the said element.
Using .textContent
will display the actual markup text instead of the desired layout, as evidenced by your experience.
The code snippet below showcases this distinction:
const emailError=document.getElementsByClassName("emailError");
emailError[0].innerHTML = '<img src="https://stackoverflow.design/assets/img/logos/sf/sf-icon.svg" /> You need to enter an e-mail address.';
emailError[1].textContent = '<img src="https://stackoverflow.design/assets/img/logos/sf/sf-icon.svg" /> You need to enter an e-mail address.';
img {
width: 40px;
aspect-ratio: 1;
}
span {
border: 1px solid black;
display: flex;
align-items: center;
}
<p>Example loaded using .innerHTML:</p>
<p><span class="emailError"></span></p>
<p>Example loaded using .textContent:</p>
<p><span class="emailError"></span></p>