Skip to content

Commit a290c92

Browse files
author
Josh Hill
committed
Flesh out getting and setting with the same method
1 parent fd34057 commit a290c92

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

js/lesson3/tutorial.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,40 @@ $("li:odd"); // all odd numbered list items
4343
$("li:first-child"); // the first child in a list
4444
```
4545

46-
##Accessing attributes `attr()`
46+
##Get and set HTML attributes `attr()`
4747

48-
Using the `attr()` method you can retrieve any element attribute.
48+
Using `attr(attributeName)` you can retrieve the value of an attribute.
49+
50+
You can use the same method to set the value of an attribute:
51+
`attr(attributeName, value)`. Many jQuery methods can be used to both get and
52+
set.
4953

5054
```js
51-
$('#logo').attr('width')
52-
$('#logo').attr('width', 300)
55+
$('#logo').attr('width') // get width
56+
$('#logo').attr('width', 300) // set width to 300
5357
```
5458

55-
##Changing CSS attributes
59+
##Get and set CSS styles `css()`
5660

57-
You can get and set the CSS properties of an element with the `css()` method.
61+
Like `attr()`, you can get and set CSS style properties with the `css()`
62+
method.
5863

5964
```js
6065
var heading = $('h1');
66+
heading.css('color');
6167
heading.css('color', 'red');
6268
```
6369

64-
##val()
70+
##Get and set input values `val()`
6571

66-
To set and get the text in an input field, you can use `val()`. Similar to `attr()` and `css()` you can use the function without any parameters to retrieve the value, and `val(value)` to update the value.
72+
Similar to `attr()` and `css()` you can use the `val()` function without any
73+
parameters to get the value of an input field, and `val(value)` to set the
74+
value.
6775

6876
To empty an input field, you can set value to an empty string.
6977

7078
```javascript
79+
$('input').val();
7180
$('input').val("");
7281
```
7382

0 commit comments

Comments
 (0)