jQuery Set Content and Attributes

Set HTML, Text, and Values.

jQuery Set Content and Attributes

We will use the same three methods from the previous chapter to set content:

  • text() - Sets or returns the text content of selected elements
  • html() - Sets or returns the content of selected elements (including HTML markup)
  • val() - Sets or returns the value of form fields

Set Attributes - attr()

The jQuery attr() method is also used to set/change attribute values.

Example

$("#test1").text("Hello world!");