Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

innerHTML not working as expected

I have searched everywhere for a solution to this problem which I cannot explain.

here is the problem, whenever I use javascript's innerHTML to inject the following string:

example:

var s = "<div><p><div><p></p></div></p></div>"

document.getElementById("id").innerHTML = s;

In Firefox, using firebug, I look at the latest markup and see <div><p></p><div><p></p></div></div>

Anyone know what this occurs?

like image 702
James Champion Avatar asked May 23 '26 17:05

James Champion


1 Answers

You cannot nest p or div inside another p. Closing the p is how the browser attempts to make your invalid HTML valid.

like image 98
Francis Avila Avatar answered May 25 '26 07:05

Francis Avila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!