You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: js/lesson3/tutorial.md
+17-8Lines changed: 17 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,31 +43,40 @@ $("li:odd"); // all odd numbered list items
43
43
$("li:first-child"); // the first child in a list
44
44
```
45
45
46
-
##Accessing attributes `attr()`
46
+
##Get and set HTML attributes `attr()`
47
47
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.
49
53
50
54
```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
53
57
```
54
58
55
-
##Changing CSS attributes
59
+
##Get and set CSS styles `css()`
56
60
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.
58
63
59
64
```js
60
65
var heading =$('h1');
66
+
heading.css('color');
61
67
heading.css('color', 'red');
62
68
```
63
69
64
-
##val()
70
+
##Get and set input values `val()`
65
71
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.
67
75
68
76
To empty an input field, you can set value to an empty string.
0 commit comments