admin管理员组

文章数量:1293557

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>";
}

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>";
}
Share Improve this question edited Feb 13, 2021 at 13:29 pretzelhammer 15.2k16 gold badges53 silver badges103 bronze badges asked Jul 24, 2018 at 1:36 DumbDevGirl42069DumbDevGirl42069 9595 gold badges25 silver badges54 bronze badges 2
  • Replace the outer double quotes of your HTML string with single quotes. – Robby Cornelissen Commented Jul 24, 2018 at 1:38
  • "Next <i class="icon icon--right icon-arrow-right"></i>" should probably be 'Next <i class="icon icon--right icon-arrow-right"></i>' – Deadweight Commented Jul 24, 2018 at 1:38
Add a ment  | 

3 Answers 3

Reset to default 4

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>';

Your HTML string should be formatted with single quotes instead of double quote.

Current is:

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

Change to:

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

You must put single quotes instead of double quote

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

本文标签: javascriptHow can I add html tags using innerHTMLStack Overflow