Module 16 · Lesson 3
DOM Manipulation
Read and change content, attributes, styles, and classes — and learn when .html() is dangerous.
Introduction
Once you've selected elements, you'll want to change them. jQuery provides concise getter/setter methods: call a method with no argument to read, or with an argument to write. This lesson covers the everyday manipulation methods and an important security rule about .html().
Pattern:
$('#el').text() reads the text; $('#el').text('new') sets it. The same method does both.Theory
Content
.text()— get/set text content. Treats input as plain text (safe)..html()— get/set inner HTML. Parses input as HTML (powerful but risky)..val()— get/set the value of form fields.
Attributes & styles
.attr('href', '...')— get/set an attribute..css('color', 'red')— get/set a CSS property (or pass an object for several).
Classes
.addClass('active'),.removeClass('active').toggleClass('active')— adds it if absent, removes it if present.hasClass('active')— returns true/false
Inserting elements
.append(content)— add to the end of the matched element..prepend(content)— add to the beginning..before()/.after()— insert outside the element..remove()/.empty()— delete elements / clear contents.
XSS warning:
.html() parses its input as HTML, so passing untrusted/user-supplied text can inject scripts. Whenever you display user input, use .text(), not .html(). Reserve .html() for trusted, developer-controlled markup.Real World Example
Type something and click the buttons. The output uses .text() so whatever you type — including <script> — is shown literally, never executed:
$('#box').text($('#nameInput').val()); // safe — input shown as text
$('#box').toggleClass('highlight'); // toggle a class
$('#box').css('color', 'crimson'); // change a style
$('#list').append('<li>New item</li>'); // trusted markup only
Output appears here…
- Existing item
Common Mistakes
- Using
.html()on user input. This is the classic jQuery XSS hole — always use.text()for untrusted data. - Confusing
.text()with.val(). Form fields use.val(); their displayed value isn't text content. - Forgetting setters affect all matched elements.
$('.x').text('hi')sets the text of every.x. - Using
.removeClass()with no argument carelessly. It strips all classes.
Best Practices
- Default to
.text(); only use.html()for markup you wrote yourself. - Use
.toggleClass()for on/off states instead of manual add/remove logic. - Pass an object to
.css({})when setting multiple properties at once. - Build complex content with element creation rather than string concatenation when user data is involved.
Practice Exercise
- Add a paragraph and a text input. On a button click, set the paragraph's text from the input with
.text(). - Add a button that toggles a
highlightclass on the paragraph. - Add a button that changes the paragraph color with
.css(). - Append a new list item to a list with
.append().
Assignment
Build a mini "comment box" demo:
- A textarea and a "Post" button.
- On click, read the textarea with
.val()and append the comment to a list — using.text()on a created<li>, never.html(). - Add a "Clear all" button that uses
.empty()on the list. - Confirm that typing
<script>alert(1)</script>shows as literal text, proving your code is XSS-safe.
Hint:
$('<li></li>').text(userInput).appendTo('#comments') safely escapes the input.Interview Questions
- What is the difference between
.text()and.html()? —.text()reads/sets plain text (escaped);.html()reads/sets parsed HTML (can execute markup). - Why is
.html()a security concern? — Passing untrusted input lets attackers inject scripts (XSS); use.text()for user data. - How do you read a form field's value? —
.val(). - What does
.toggleClass()do? — Adds the class if it's missing, removes it if present. - How do you add an element to the end of a container? —
.append()(or.appendTo()).
Additional Resources
- api.jquery.com/category/manipulation — manipulation methods
- api.jquery.com/category/attributes — attribute and class methods
- owasp.org — Cross-Site Scripting (XSS) prevention guidance