Aral Code

Changing the Page Lesson 6 of 10

jQuery — Getting and Setting Text, HTML, Values and Attributes

Read and change content with jQuery — text(), html(), val(), attr(), prop() and data() — with a live form preview and a theme switcher.

Many jQuery methods work both ways: call them without an argument to get a value, and with an argument to set it.

text(), html() and val()

Example Get and set
<script src="/vendor/jquery.min.js"></script>
<h3 id="title">Grade 10 – Rizal</h3>
<input id="nameBox" value="Juan Dela Cruz">
<p id="out"></p>
<button id="read">Read values</button>
<button id="write">Change values</button>
<script>
  $("#read").on("click", function () {
    $("#out").text("Title: " + $("#title").text() + " | Input: " + $("#nameBox").val());
  });
  $("#write").on("click", function () {
    $("#title").html("Grade 10 – <em>Bonifacio</em>");
    $("#nameBox").val("Maria Clara");
    $("#out").text("Changed!");
  });
</script>
MethodGets / sets
.text()Plain text content (safe for user input)
.html()HTML content
.val()Value of inputs, selects and textareas

A live preview

Example Live ID card preview
<script src="/vendor/jquery.min.js"></script>
<style>
  .idcard { width: 230px; padding: 12px; border-radius: 12px; background: #0f5c4d; color: white; font-family: sans-serif; margin-top: 10px; }
  .idcard small { opacity: .8; }
</style>
<input id="n" placeholder="Name"> <select id="g"><option>Grade 7</option><option>Grade 8</option><option>Grade 9</option><option>Grade 10</option></select>
<div class="idcard">
  <small>RIZAL NATIONAL HIGH SCHOOL</small>
  <h3 id="cardName">Your name</h3>
  <span id="cardGrade">Grade 7</span>
</div>
<script>
  $("#n").on("input", function () {
    $("#cardName").text($(this).val() || "Your name");
  });
  $("#g").on("change", function () {
    $("#cardGrade").text($(this).val());
  });
</script>

attr() and prop()

Example Changing attributes
<script src="/vendor/jquery.min.js"></script>
<img id="pic" src="/img/mango.svg" alt="Mango" width="100">
<p><a id="link" href="https://en.wikipedia.org/wiki/Mango">About mangoes</a></p>
<label><input type="checkbox" id="agree"> I agree</label>
<button id="submit" disabled>Submit</button>
<script>
  $("#pic").on("click", function () {
    const isMango = $(this).attr("src").includes("mango");
    $(this).attr({ src: isMango ? "/img/halo-halo.svg" : "/img/mango.svg", alt: isMango ? "Halo-halo" : "Mango" });
  });
  $("#agree").on("change", function () {
    $("#submit").prop("disabled", !$(this).prop("checked"));
  });
</script>
  • .attr("name") reads an HTML attribute; .attr("name", value) sets it. Pass an object to set several at once.
  • .prop() reads or sets properties like checked, disabled and selected. Use prop for true/false states, not attr.

data(): custom data attributes

Example Reading data-* attributes
<script src="/vendor/jquery.min.js"></script>
<button class="item" data-price="25" data-name="Pandesal">Pandesal</button>
<button class="item" data-price="8" data-name="Kape">Kape</button>
<p id="total">Total: ₱0</p>
<script>
  let total = 0;
  $(".item").on("click", function () {
    total += $(this).data("price");             // reads data-price as a number
    $("#total").text("Total: ₱" + total + " (last: " + $(this).data("name") + ")");
  });
</script>

data-* attributes are a clean way to store extra information in HTML, like a product's price or id, for your scripts to use.

Working with many elements

Setters apply to every matched element. Getters return the value of the first one. To read all of them, use .each() or .map():

$(".price").text("₱0");                         // sets all
const first = $(".price").text();               // careful: text() joins all text
const names = $(".name").map(function () { return $(this).text(); }).get();

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. How do you read the value typed in an input?
2. Which is safest for showing user input?
3. Which should you use to disable a button?
4. What does $(this).data("price") read?

By Sir Reyel · Last updated 2026-12-04