Which one is correct?
Hello,
Where should I append a created element?
https://codepen.io/lanszelot/pen/XJrvRjJ
15th or 17th line is the correct?
Both of them do the same result, but I do not know which one is correct and why.
35 Replies
outside the for loop. You're building the element in the for loop, then append it after, otherwise you're appending it every time the for loop runs. It works the same because you're re-appending the same element, I think
Pretty much this ☝️
You're only creating the element once so each time you try to append it it's already there so nothing really happens.
If you append the element before the loop you get the same result as the inner loop is only changing the content of the paragraph:
Also, since you're only changing the inner text of the
p
element you shouldn't be using .innerHTML
, as that has a few extra steps to parse and look for HTML elements. You should be using .textContent
instead.
-# Would you like to know more?First of all: thank you so much to everyone.
If I am using there is something less in css.
-# you can use single backticks for inline code formatting: `test` =
test
| and continue on with non-formatted text
I don't understand what you mean hereNot in this case, but I can use for everything, this is why I use that all the time.
and have limits
I create a problem in the past and looking for hours what did I wrong, after I learned, I am not using the other two
I am sure you have right, but I am a beginner, and I am coding only for fun, nothing else. I did not create huge projects.
If you read the link I sent you you'll learn why you shouldn't use
.innerHTML
. But the short answer is performance and safety. .innerHTML
parses looking for HTML elements, which is slow. Also, if used with user-created content it can cause massive security vulnerabilities.
Yes, you're a beginner. That's why I'm teaching you the right tool for the job. Learn good habits now and you won't make big mistakes later b/c you know better.
* .innerHTML
is dangerous and should only be used with strings that have HTML elements and never with unsanitized user input
* .innerText
is for human-readable text only, but takes styling into account and can cause a complete page reflow (bad for performance)
* .textContent
gets all text content, including script and style tags, but only sets text—no reflow
Knowing the difference between the three is very importantThank you so much
I tried, and the site not works, because I am using Unicode Character HTML Entity:
You could just use the emoji, y'know
If you must use the HTML character code and can't use the emoji for some reason then you should make the entire string first then set the inner HTML once.
Thank you, I try it
did you try adding a
;
at the end?
and if you really want to use that html entity, just use String.fromCodePoint(128145)
, if you want to add it in js
then you can use it with .textContent
, as you shouldHey, @ἔρως, stop with the logic and good code practices! lol
lol
just casually dropping a bag of truths
also, you are concatenating to
.innerHTML
Yeah, each loop O_O
i cant imagine how bad the parser is performing at that point
poor thing has to parse your text multiple times per frame
just concatenate it all into a string, and then send it all in one go: the browser will thank you
So will the end users
yup, and if you are lazy, you can just shove everything into an array and join with a space, at the end
y'all forgetting
String.prototype.repeat()
?Yes, yes I am. Why do you ask? lol
'💑'.repeat(numbers) would work way better than a for loop
no, im not
the output is a letter and the emoji
unless you use
replace
with a function, it wont work
oh wait 🤦
i just described how to make it work ...
... it's too friday for me 😦this one is the easiest and works well, thanks
you're welcome
but please, stop using
.innerHTML
i cant find a good reason to use it in 2015, let alone 2025HTMX, since it's returning an HTML string
but you have a proper html parser
the parser doesnt execute javascript, like how
.innerHTML
foes
does
the dom parser has been available for a long timeIt's your own server, so I'm assuming that the HTML string is trusted and sanitized, so it's NBD
But yes, you can
DOMParser.parseFromString()
if you want, then .appendChild()
exactly, its simple
I was just advocating for the devil, as they say
also, if you have a bug or your database is compromised and sends bad html
having the parser is good
I'd never use
.innerHTML
either.i know what you mean
but ive seen editor throwing chunks of <style> tags and breaking the site
so, i dont trust my own server
If I can, I
document.createElement()
and do all the needed things, then do one append with that element (if it's client-side generation)
🤢or use a template tag
🤢 indeed
Indeed. Use the right tool for the job
but people just do the "easy" or actually dont know better