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,783 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,783 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>");