In this chapter, we will explore how to set content using jQuery. This includes modifying text, HTML content, form values, and attributes of HTML elements. By understanding these techniques, you can dynamically update the content of your web pages based on user interactions or other events.
Setting content is a fundamental aspect of web development that allows you to dynamically change the appearance and behavior of your web pages. Whether you’re updating text based on user input, modifying HTML structures, or setting form values, jQuery provides a straightforward and efficient way to achieve these tasks.
jQuery offers a variety of methods to set different types of content:
.text()
.html()
.val()
.attr()
.css()
.text()
MethodThe .text()
method sets the text content of the specified elements.
$(selector).text(newText);
Set Text Content
Original text
.text()
method sets the text content of the paragraph to “Updated text content!” when the button is clicked..html()
MethodThe .html()
method sets the HTML content of the specified elements.
$(selector).html(newHtml);
Set HTML Content
Original HTML content
.html()
method sets the HTML content of the #contentDiv
to a new paragraph with bold text when the button is clicked.#contentDiv
contains: <p>Original HTML content</p>
.#contentDiv
updates to: <p>Updated <strong>HTML</strong> content!</p>
.val()
MethodThe .val()
method sets the value of form elements such as input, select, and textarea.
$(selector).val(newValue);
Set Form Value
.val()
method sets the value of the text input field to “New input value” when the button is clicked..attr()
MethodThe .attr()
method sets the value of the specified attribute for the selected elements.
$(selector).attr(attributeName, newValue);
Set Attribute Value
.attr()
method sets the src
attribute of the #image
element to “new_image.jpg” when the button is clicked..css()
MethodThe .css()
method sets one or more CSS properties for the selected elements.
// Set a single CSS property
$(selector).css(propertyName, value);
// Set multiple CSS properties
$(selector).css({
property1: value1,
property2: value2,
// ...
});
Set CSS Properties
.css()
method sets multiple CSS properties for the #colorBox
element when the button is clicked.#colorBox
has a blue background and dimensions of 100×100 pixels.#colorBox
updates to a red background and dimensions of 200×200 pixels.jQuery’s .data()
method allows you to set custom data attributes (using the HTML5 data-*
attributes).
$(selector).data(attributeName, value);
Set Data Attributes
Hover over me
.data()
method sets the value of the data-info
attribute for the #dataDiv
element to “Updated Information” when the button is clicked.#dataDiv
has a data-info
attribute value of “Initial Information”.data-info
attribute updates to “Updated Information”, and the output paragraph displays: “Data attribute updated to: Updated Information”.text()
method sets the text content of elements..html()
method sets the HTML content of elements..val()
method sets the value of form elements..attr()
method sets the value of attributes..css()
method sets CSS properties..data()
method sets custom data attributes.Setting content with jQuery is a powerful way to dynamically update the text, HTML, values, attributes, and CSS properties of HTML elements. By using methods such as .text(), .html(), .val(), .attr(), .css(), and .data(), you can create interactive and dynamic web pages that respond to user input and other events.Happy coding !❤️