DOM Manipulation
Most jQuery code reads or changes something on the page: a heading’s text, an input’s value, a link’s href. One rule makes it easy: call a method with no value to read, with a value to write.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
Learning objectives#
- Read and set text, HTML and input values
- Use .attr() vs .prop() correctly
- Store and read data-* values with .data()
- Avoid XSS by preferring .text() for user input
Explanation#
.text() reads/sets plain text; .html() reads/sets HTML markup; .val() reads/sets form field values.
Getter vs setter: $("#title").text() returns the text of the first match; $("#title").text("Hi") sets it on every match and returns the jQuery object for chaining.
.attr("href") works with HTML attributes as written; .prop("checked") / .prop("disabled") work with live DOM properties. Use .prop for checked, selected and disabled.
.data("price") reads data-price="499" and converts it to a number; .data("x", value) stores values in jQuery’s cache (it does not change the HTML attribute).
Never put user input into .html(): a name like <img src=x onerror=alert(1)> would run. Use .text(), which escapes it.
Syntax#
$('#title').text(); // read
$('#title').text('New title'); // write
$('#email').val('a@b.com');
$('#link').attr('href', '/home');
$('#agree').prop('checked', true);
$('#card').data('price'); // data-price="499" → 499
Code example: Reading and writing a product card#
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<div id="card" data-price="499" data-stock="3" style="font-family:sans-serif">
<h3 class="name">Notebook</h3>
<a class="more" href="/products/1">Details</a>
<label><input type="checkbox" class="gift"> Gift wrap</label>
<input class="qty" value="1" size="3">
<p class="note"></p>
</div>
<script>
$(function () {
const $card = $('#card');
console.log('Name:', $card.find('.name').text());
console.log('Price:', $card.data('price'), typeof $card.data('price'));
console.log('Link:', $card.find('.more').attr('href'));
$card.find('.name').text('Spiral Notebook');
$card.find('.qty').val(2);
$card.find('.gift').prop('checked', true);
console.log('Gift?', $card.find('.gift').prop('checked'));
const userInput = '<b>Asha</b>';
$card.find('.note').text('Ordered by ' + userInput); // shown as plain text, not bold
});
</script>
Name: Notebook Price: 499 number Link: /products/1 Gift? true (The heading becomes “Spiral Notebook”, quantity shows 2, the checkbox is ticked, and the note shows the literal text “Ordered by <b>Asha</b>”.)
Step-by-step explanation#
- .text() with no argument reads the heading.
- .data("price") converts "499" to the number 499.
- .attr("href") reads the attribute exactly as written.
- Setters (.text("..."), .val(2), .prop("checked", true)) update the page.
- .text() escapes the user input, so the <b> tag is shown, not executed.
Real-world use case#
A shopping cart reads data-price and data-stock from each product card, updates quantity inputs with .val(), and shows totals with .text().
Common mistakes#
The attribute shows the initial state, not the current one. Use .prop("checked").
Inserting untrusted text as HTML enables XSS. Use .text().
.data("x", 1) stores in memory; data-x in the HTML is unchanged. Use .attr("data-x", 1) if CSS selectors need it.
Best practices#
- .text() for user content
- .prop() for checked/disabled/selected
- data-* attributes for values the script needs
- Chain setters on one selection
Remember this#
No argument = get. With argument = set.
attr = what the HTML says. prop = what the element is now.
User input → .text(), never .html().
Practice exercise#
Your task: An input #price and a button #apply: on click, read the price, add 18% GST, and show it in #total formatted to 2 decimals.
Need a hint?
Show solution
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<input id="price" value="100"> <button id="apply">Add GST</button>
<p>Total: <span id="total">–</span></p>
<script>
$(function () {
$('#apply').on('click', function () {
const price = parseFloat($('#price').val()) || 0;
$('#total').text((price * 1.18).toFixed(2)); // 118.00
});
});
</script>
Mini quiz#
Which call reads the current value of an input?
Want more? Practise all jQuery MCQs · jQuery interview questions
My notes#
Saved in this browser.