jQuery Get


Some jQuery methods allow you to assign or read a value from a selection. text(), html(), attr(), and val() are a few examples.

When these methods are called with no arguments, they are referred to as getters since they get (or read) the element's value. When these methods are used with a value as an argument, they are known as setters because they set (or assign) that value.


jQuery text() Method

The jQuery text() function is used to either retrieve or set the combined text contents of the chosen components, including their descendants.

Get Contents with text() Method

The following example demonstrates how to obtain the text contents of paragraphs:

Example

Preview

jQuery html() Method

The html() function of jQuery is used to obtain or set the HTML contents of elements.

Get HTML Contents with html() Method

The following example demonstrates how to obtain the HTML contents of paragraph elements as well as an element container:

Example

Preview

jQuery attr() Method

The jQuery attr() function may be used to retrieve the value of an element's attribute or to set one or more attributes for the specified element.

Get Attribute Value with attr() Method

The following example demonstrates how to obtain the href attribute of a hyperlink, i.e. the <a> element, as well as the alt attribute of an image:

Example

Preview

jQuery val() Method

The val() function of jQuery is mostly used to acquire or set the current value of HTML form elements such as <input>, <select>, and <textarea>.

Get the Values of Form Fields with val() Method

The following example demonstrates how to obtain the values of form controls:

Example

Preview