jQuery Get or Set

In this tutorial you will learn how to use jquery get & set in the element’s content and attribute value as well as the from control value using jQuery.

jQuery Get or Set Contents and Values

Some jQuery methods can be used to either assign or read some value on a selection. A few of these methods are text(), html(), attr(), and val(). These methods are used for “jquery get & set”.

When these methods are called with no argument, it is referred to as a getters, because it gets (or reads) the value of the element. When these methods are called with a value as an argument, it’s referred to as a setter because it sets (or assigns) that value.

jQuery text() Method

The jQuery text() method is either used to get the combined text contents of the selected elements, including their descendants, or set the text contents of the selected elements. This is part of jquery get & set methods

Get Contents with text() Method

The following example will show you how to get the text contents of paragraphs:

Note:The jQuery text() retrieves the values of all the selected elements (i.e. combined text), whereas the other getters such as html(), attr(), and val()returns the value only from the first element in the selection.

Set Contents with text() Method

The following example will show you how to set the text contents of a paragraph:

Note:When the jQuery text(), html(), attr(), and val() methods are called with a value as an argument it sets that value to every matched element.


jQuery html() Method

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

Get HTML Contents with html() Method

The following example will show you how to get the HTML contents of the paragraph elements as well as a <div> element container:

Note:If multiple elements are selected, the html() method only returns the HTML contents of the first element from the set of matched elements.

Set HTML Contents with html() Method

The following example will show you how to set the HTML contents of the <body> element:

jQuery attr() Method

You can use the jQuery attr() method to either get the value of an element’s attribute or set one or more attributes for the selected element.

Get Attribute Value with attr() Method

The following example will show you how get the href attribute of the hyperlink i.e. the <a> element as well as the alt attribute of an <img> element:

Note:If multiple elements are selected, the attr() method only returns the attribute value of the first element from the set of matched elements.

Set Attributes with attr() Method

The following example will show you how to set the checked attribute of the checkbox.

The attr() method also allows you to set multiple attributes at a time. The following example will show you how to set the class and title attribute for the <img> elements.

jQuery val() Method

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

Get the Values of Form Fields with val() Method

The following example will show you how to get the values of form controls:

Note:If multiple form elements are selected, the val() method only returns the value of the first element from the set of matched elements.

Set the Values of Form Fields with val() Method

The following example will show you how to set the values of the form controls:

jquery get & set

from the above topic, we understand how to use “jquery get & set“. If You have any queries, you can ask those in the Forum section of our site. Here is the link for Forum section