JavaScript jQuery Basics Understanding jQuery Events and DOM Traversal Adding New Elements to the DOM

I did not understand why we are creating a button in this way?

Button is already created, why we are creating in different way?

2 Answers

Steven Parker
Steven Parker
173,876 Points

The JavaScript button is associated with a different functionality than the button already present from HTML. So the code shown in the video replaces the other button with the new one. The advantage of having the HTML button there to start with is for cases where JavaScript is not supported by the user's browser (or disabled by their settings).

Adam Cameron
Adam Cameron
Python Web Development Techdegree Graduate 16,730 Points

If a user pulls up the page and their JavaScript doesn't load/doesn't work for whatever reason, the <button> won't work for them because the stuff controlling it is in our app.js (which isn't working for this user). Terrible user experience, in fact the app completely fails for that user.

On the other hand, if we make the presence of the button dependent on JavaScript working in the first place, then the worst thing that can happen for a user is they don't get to experience the fun of pressing the button and 'revealing' the spoiler for themselves. But at least they still get to see the spoiler. Does that make sense?