Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Jiten Mehta
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jiten Mehta
Front End Web Development Techdegree Graduate 21,209 Points

Adding new elements to the DOM code challenge

Hi guys, I can't see where I'm going wrong here.

The code challenge is asking me to add in a new li element containing the student name Sam Smith using Jquery.

When I preview the HTML, the name has been added to the ul. Why do I keep failing?

Help!

This is the code I put in

const $newStudent = $('.student-list').append('<li>Sam Smith</li>');

1 Answer

Louise St. Germain
Louise St. Germain
19,412 Points

Hi Jiten,

The challenge is expecting that the variable $newStudent contain only the new element created, and that you will then use that variable name to append it to the list.

If you got to step 2 of the challenge, I assume that you correctly figured out the first part, creating the new element:

const $newStudent = $('<li>Sam Smith</li>');

So from here, just add a second line where you append $newStudent to the class .student-list, like in the video Adding New Elements to the DOM. In other words, just break your previous code into two steps.

I hope this helps!