Skip to content

Commit 5eaa312

Browse files
lady3beangnarf
authored andcommitted
Using jQuery: Clarifies distinctions between getter and setter methods
Fixes gh-655 Closes gh-656
1 parent 012d7ab commit 5eaa312

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

page/using-jquery-core/working-with-selections.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55

66
### Getters & Setters
77

8-
jQuery "overloads" its methods, so the method used to set a value generally has the same name as the method used to get a value. When a method is used to set a value, it's called a setter. When a method is used to get (or read) a value, it's called a getter. Setters affect all elements in a selection. Getters get the requested value only for the first element in the selection.
8+
Some jQuery methods can be used to either assign or read some value on a selection. When the method is called with a value as an argument, it's referred to as a setter because it sets (or assigns) that value. When the method is called with no argument, it gets (or reads) the value of the element. Setters affect all elements in a selection, whereas getters return the requested value only for the first element in the selection, with the exception of [`.text()`](http://api.jquery.com/text/), which retrieves the values of all the elements.
99

1010
```
11-
// The .html() method used as a setter:
11+
// The .html() method sets all the h1 elements' html to be "hello world":
1212
$( "h1" ).html( "hello world" );
1313
```
1414

1515
```
16-
// The .html() method used as a getter:
16+
// The .html() method returns the html of the first h1 element:
1717
$( "h1" ).html();
18+
// > "hello world"
19+
1820
```
1921

2022
Setters return a jQuery object, allowing you to continue calling jQuery methods on your selection. Getters return whatever they were asked to get, so you can't continue to call jQuery methods on the value returned by the getter.

0 commit comments

Comments
 (0)