jQuery Get Content and Attributes
A big part of DOM work is reading what is on the page: the text of a heading, the value a user typed, the URL of a link. jQuery uses the same methods for reading and writing — called without a value, they are getters.
The four content getters
| Method | Returns |
|---|---|
.text() |
The combined text of all matched elements and their descendants, without tags |
.html() |
The inner HTML (markup included) of the first matched element |
.val() |
The current value of the first form field (input, select, textarea) |
.attr(name) |
The value of an HTML attribute on the first element |
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
<p id="p1">This is <b>bold</b> and <i>italic</i>.</p>
<input id="name" value="Ana">
<a id="link" href="https://jquery.com" title="jQuery home">jquery.com</a>
<br><br>
<button id="read">Read everything</button>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$("#read").on("click", function () {
console.log("text():", $("#p1").text());
console.log("html():", $("#p1").html());
console.log("val():", $("#name").val());
console.log("attr('href'):", $("#link").attr("href"));
console.log("attr('title'):", $("#link").attr("title"));
console.log("missing attr:", $("#link").attr("target"));
});
</script>
</body>
</html>Edit the input, click again, and val() shows the new value. A missing attribute returns undefined.
text() vs html()
.text() strips all tags and returns only what a reader sees. .html() keeps the markup. One more difference: .text() on several elements joins all their text, while .html() only looks at the first:
// <li>One</li><li>Two</li>
$("li").text(); // "OneTwo"
$("li").html(); // "One"attr() vs prop()
HTML attributes are what is written in the markup. DOM properties are the live values on the element object. For most things they match, but for state that the user can change — checked, selected, disabled, and an input's value — they differ.
| You want | Use |
|---|---|
What the HTML says (href, src, data-*, title, aria-*) |
.attr() |
The current state (checked, selected, disabled) |
.prop() |
| The current value of a field | .val() |
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
<label><input type="checkbox" id="news"> Subscribe to newsletter</label>
<br>
<a id="lnk" href="page.html">relative link</a>
<br><br>
<button id="check">Compare</button>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$("#check").on("click", function () {
const $box = $("#news");
console.log("attr('checked'):", $box.attr("checked")); // from the markup
console.log("prop('checked'):", $box.prop("checked")); // live state
console.log("is(':checked'):", $box.is(":checked"));
// href: attribute vs resolved property
console.log("attr('href'):", $("#lnk").attr("href"));
console.log("prop('href'):", $("#lnk").prop("href"));
});
</script>
</body>
</html>Tick the checkbox and press Compare: attr("checked") stays undefined because the markup never changed, while prop("checked") is true. Also notice that prop("href") returns the fully resolved URL.
Reading data attributes
Custom data-* attributes are a clean way to store extra information on elements. .data(key) reads them and converts types (numbers, booleans, JSON):
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
.product { border: 1px solid #ccc; padding: 8px; margin: 6px 0; width: 260px; cursor: pointer; border-radius: 6px; }
</style>
</head>
<body>
<div class="product" data-id="17" data-price="24.5" data-in-stock="true" data-tags='["coffee","beans"]'>
Espresso Beans
</div>
<div class="product" data-id="23" data-price="9" data-in-stock="false" data-tags='["tea"]'>
Green Tea
</div>
<p id="info">Click a product.</p>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$(".product").on("click", function () {
const $p = $(this);
const price = $p.data("price"); // number 24.5
const inStock = $p.data("inStock"); // boolean (data-in-stock → inStock)
const tags = $p.data("tags"); // array parsed from JSON
$("#info").text($p.text().trim() + ": quot; + price.toFixed(2) + (inStock ? "" : " (sold out)"));
console.log("id", $p.data("id"), typeof $p.data("id"), "| tags", tags);
console.log("attr gives a string:", typeof $p.attr("data-price"));
});
</script>
</body>
</html>Getter reference
| Method | Reads from | Notes |
|---|---|---|
.text() |
all matched elements | Tags removed, text concatenated |
.html() |
first element | Includes child tags |
.val() |
first form field | Multi-select returns an array |
.attr("name") |
first element | String or undefined |
.prop("name") |
first element | Live DOM value (boolean, string, …) |
.data("key") |
first element | data-* values, type-converted |
.css("prop") |
first element | Computed style value (next lessons) |
Tip: Getters read only from the first element (except
.text()). To get a value from each element, use.map(function () { return $(this).val(); }).get().
Test yourself
For <p>Hi <b>there</b></p>, what does $("p").html() return?
Hi thereHi <b>there</b><p>Hi <b>there</b></p>there
Which method reliably tells you whether a checkbox is currently ticked?
.attr("checked").prop("checked").val().text()
What type does .data("price") return for data-price="9.5"?
- String
- Number
- Boolean
- Array