Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript DOM Scripting By Example Adding and Removing Names RSVP Checkbox

Sarah Breidenbach
Sarah Breidenbach
4,490 Points

What is the advantage of creating an element with JavaScript instead of just making it in the HTML in the first place?

I'm doing the DOM Scripting By Example course and we're creating a lot of elements in Javascript.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Yes, you are creating a lot of elements. But web pages and web apps these days have to be very flexible. Imagine for a moment that you have a website that does some sort of project tracking like Asana or Trello where users can add a to-do list.

How would you write that in HTML? You have no way to know how many items they might want to add. Or how many friends/contacts/team members etc. The problem is that in this instance you cannot know in advance what the HTML would need to look like. And don't forget it will be different for user A than it will be for user B. User A might have 3 projects while user B has 20.

To make your app/site as flexible as possible, your page needs to be able to change on the fly.

Hope this helps! :sparkles:

Jonathan Santiago
seal-mask
.a{fill-rule:evenodd;}techdegree
Jonathan Santiago
Full Stack JavaScript Techdegree Student 7,998 Points

Thanks for clearing that up Jennifer! I was wondering the same thing when I got to this video, but what you say makes a lot of sense. Said "Duh!" after I read it lol