Skip to content

Commit

Permalink
Using jQuery: Clarifies distinctions between getter and setter methods
Browse files Browse the repository at this point in the history
Fixes gh-655
Closes gh-656
  • Loading branch information
lady3bean authored and Krinkle committed Apr 9, 2024
1 parent 298e19c commit e829be4
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions page/using-jquery-core/working-with-selections.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@

### Getters & Setters

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.
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.

```
// The .html() method used as a setter:
// The .html() method sets all the h1 elements' html to be "hello world":
$( "h1" ).html( "hello world" );
```

```
// The .html() method used as a getter:
// The .html() method returns the html of the first h1 element:
$( "h1" ).html();
// > "hello world"
```

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.
Expand Down

0 comments on commit e829be4

Please sign in to comment.