May 07, 2021 jQuery
1. jQuery - Set content and properties
2. Set content - text(), html(), and val()
3. the callback functions for text(), html(), and val().
We'll set up the content using three of the same methods in the previous chapter:
text() - Set or return the text content of the selected element
html() - Set or return the contents of the selected element (including HTML tags)
val() - Set or return the value of the form field
The following example shows how to set up content with text(), html(), and val():
The three jQuery methods above: text(), html() and val(), also have callback functions. T he callback function has two parameters: the subsector of the current element in the list of selected elements, and the original (old) value. Then return the string you want to use with the new value of the function.
The following example demonstrates text() and html() with callback functions:
The jQuery attr() method is also used to set/change property values.
The following example shows how to change the value of the href property in the (settings) link:
The attr() method also allows you to set multiple properties at the same time.
The following example shows how to set both the href and title properties:
Tip: Please refer to this site for more information about the jQuery attr() method!
The jQuery method attr(), which also provides a callback function. T he callback function consists of two parameters: the subsector of the current element in the list of selected elements, and the original (old) value. Then return the string you want to use with the new value of the function.
The following example demonstrates an attr() method with a callback function:
For jQuery, the ability to operate the DOM is important. jQuery provides a range of DOM-related methods that make it easy to access and manipulate elements and properties: refer to the "jQuery - DOM Operations" section of this site for details.