jQuery·jQuery from selection to migration

DOM manipulation

Update text, HTML, classes and children without clobbering state.

Beginner 13 min read Lesson 3 of 300 Challenge included
What you will be able to do
  • Use .text for untrusted strings.
  • Use .html only for trusted markup.
  • Use .append and .prepend to add content.
  • Use .addClass, .removeClass and .toggleClass for state.

In plain English

jQuery’s .text sets text safely, .html parses markup and .append adds nodes. Prefer .text for user data. When replacing a list, preserve focus and selection where possible.

dom.jsJavaScript
$('#status').text('Saved')
$('#list').append($('<li>').text('New lesson'))
$('#panel').toggleClass('is-open')
Your turn

Append a list item

Easy

Append an li with the text "CSS" to #lessons.

JAVASCRIPT · starter
$('#lessons').append()
Check your understanding before moving on.
Knowledge checkQuestion 1

Which method safely inserts plain text?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Events and delegation
Lesson 3 of 300 · 0% of course