</>
Skip to content
jQuery lessons (15/39)

jQuery — HTML

text()

Get or set text content:

// Get
var text = $("p").text();

// Set
$("p").text("New text");

html()

Get or set HTML content:

// Get
var html = $("div").html();

// Set
$("div").html("<strong>Bold text</strong>");

val()

Get or set form values:

// Get
var value = $("input").val();

// Set
$("input").val("Hello");

// Set for select
$("select").val("option2");

attr()

Get or set attributes:

// Get
var href = $("a").attr("href");

// Set
$("img").attr("src", "new-image.jpg");
$("a").attr("target", "_blank");

prop()

For boolean properties:

// Get
var checked = $("input:checkbox").prop("checked");

// Set
$("input:checkbox").prop("checked", true);
$("input:checkbox").prop("disabled", true);

removeAttr()

$("a").removeAttr("target");

text vs html vs val

MethodUse For
text()Plain text
html()HTML content
val()Form inputs

Mini Practice

  1. Get and set text content
  2. Get and set an HTML string
  3. Get and set a form value
  4. Toggle a checkbox's checked property

Up Next

Continue with Get Content — retrieving content from elements.

Related Topics

Frequently Asked Questions about HTML

What is HTML in jQuery?

HTML is a fundamental concept in jQuery. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn HTML?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn HTML.

Why is HTML important in jQuery?

HTML is essential for jQuery development. Understanding this concept will help you write better code and solve real-world problems more effectively.