Set Content and Attributes in jQuery

Text(), html(), and val() are methods for setting content ()

To create content, we'll use the same three methods as on the previous page:

Sets or returns the text content of selected elements with text().

html() - Sets or retrieves the content of a set of elements (alongside HTML markup)

val() is a function that defines or gives an output of required fields.

Using the jQuery text(), html(), and val() methods, the following example shows how to set content:

Illustration:

EXAMPLE ❯


Text(), html(), and val() have a callback function ()

Text(), html(), and val() are all jQuery methods that have a callback function. This same callback function takes two parameters: the index of the entered by the user element in the list of components and the previous (old) value. The string you would like to use as the specific value is then returned from the component.

Text() and html() are used with a method call in the given description:

Illustration:

EXAMPLE ❯


Attributes - set attr ()

To set/change attribute values, use the jQuery attr() method.

The image below illustrates how else to adjust (set) the href parameter value in a connection:

Illustration:

EXAMPLE ❯


You can also use the attr() method to set multiple attributes at once.

Setting both the href and title attributes simultaneously time is shown in illustration:

Illustration:

EXAMPLE ❯


A callback function for the variable attr ()

This same jQuery method attr() contains a callback function. The callback function takes two criteria: the indicators of the currently selected element in the list of elements and the initial (old) attribute value. The function then returns the string you want to use as the new attributes.

This same mentioned example uses attr() in combination with something like a callback function:

Illustration:

EXAMPLE ❯