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 jQuery Basics Understanding jQuery Events and DOM Traversal Adding New Elements to the DOM

Timothée Couchoud
Timothée Couchoud
2,150 Points

Why my function append() does not work?

Why my function append() does not work?

Could somebody help me? Thanks!

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
    <h2>Student List</h2>

    <ul class="student-list">
        <li>James McAvoy</li>
        <li>Alena Holligan</li>
        <li>Wade Christensen</li>
        <li>Matt Krzyzynski</li>
    </ul>

    <script
    src="jquery-3.2.1.min.js"></script>
    <script src="app.js"></script>
</body>
</html>
app.js
const $newStudent = $('ul.student-list').append("<li>Sam Smith</li>");

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

You need to MDN Create an Element in the dom first, and then append that newly created element.

Timothée Couchoud
Timothée Couchoud
2,150 Points

Thanks Dave, the exercise is given as follwing and it is asked to use JQUERY:

"In app.js, use jQuery to create a new <li> element containing the student name "Sam Smith", and save your new element to a variable called $newStudent."

So, normally it should directly work with https://api.jquery.com/append/#append-content-content.

I still not understand where my error is...

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Yes, you are correct, your jQuery statement does work in the real world (and my stuff above doesn't apply to your question).

You are just 1 step a head of the challenge - the first task just wants you to create the variable and then the next will will have you do the append, using the variable instead of the hard coded argument.

So, task 1 just needs:

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