Can someone explain why you would use .append?

From what I understand it allows you to add elements to your HTML page.

I don’t understand what the purpose of this is? Why would you not just simply write it up in the HTML instead? I’m sure I’m missing something here :grimacing:

you have to be God I think to write out every possible word that can be needed in an HTML.

Think about when you login to a banking app for eg.
If you get the password wrong, you see a message about it.
BUT if you keep getting it wrong, maybe you get a warning about how many tries you have left.
And then the account gets locked.

You could write out all these messages and hide them in the html, or you could generate them based on the situation and the user.

Other examples:
When you click the Ask For Help button in fCC, it takes your code and sticks it into a template for the forum. That may be a number of different appends.

Or, you have some kind of editing app online say google docs
and you want to react to a person asking to insert a table or a picture our something in the doc. You must “append” that into the html. You cannot predict what the user will want there…

I’m sure there are better examples out there.

2 Likes

Imagine you need to add 80 or so similar elements to your page.

If and only if user clicked some fancy button on your page.

Kinda need to check some condition.
And it would be nice to use some looping to append all that :upside_down_face:

That would be 10 or so lines of code in JS(can vary depending on details of course)

In HTML that would be much more, and no way to change stuff dynamically.

1 Like

Thanks guys that cleared up a lot!