4 ms·
.innerHTML is great for wiping parts (or the entire) DOM. But yah, should definitely not be used with anything other than an empty string. Example: docume
by michaelmcmillan 7y ago
.innerHTML is great for wiping parts (or the entire) DOM. But yah, should definitely not be used with anything other than an empty string.
Example:
document.innerHTML = '';
- rndgermandude 7y agoDon't. Use .textContent = "";
- oauea 7y agoWhy?
- rndgermandude 7y agoBecause that way you don't have any .innerHTML in your code, which makes it easier to analyze and requires fewer eslint expectations, etc. And it has the same end result. Also, technically, this should be faster since that empty string is not put through the html parser (tho browsers might optimize for this special case already).
- michaelmcmillan 7y agoSounds like premature optimization to me. An empty string will take constant time. Much more concerned with communicating intent, and .textContent is just confusing.
- rndgermandude 7y agoNot an optimization, like I said, but to avoid having innerHTML in your code. innerHTML isn't that much better communicating intent, either. That's what comments are for.