Skip to content

Commit d126dc5

Browse files
committed
add interacting with a collection
1 parent 8d3e426 commit d126dc5

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

docs/user-guide.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,11 +1057,36 @@ paragraphs = pydom['p']
10571057

10581058
In the example above, `paragraphs` is an `ElementCollection` that maps to all `p` elements in the page.
10591059

1060+
As any collections, `ElementCollection` can be used to iterate over a collection of elements or to pick
1061+
specific elements or slices of elements in the collection. For instance:
10601062

1063+
```python
1064+
for element in paragraphs:
1065+
display(element.html)
1066+
1067+
# let's now display only the last 2 elements
1068+
for element in paragraphs[-2:]:
1069+
display(element.html)
1070+
```
10611071

10621072
##### Interacting with an ElementCollection
10631073

1064-
Interacting
1074+
Besides from allowing operations as an iterable object, `ElementCollection` objects also offer a few
1075+
convenient methods to directly interact with th elements in the collection. For instance, it's possible
1076+
to ask for specific attributes of the elements in the collection directly:
1077+
1078+
```python
1079+
display(paragraphs.html)
1080+
```
1081+
1082+
The example above displays a list with the value of the `html` attribute for all the elements in the
1083+
`paragraphs` collection.
1084+
1085+
The same way we can read attributes, we can also set an attribute directly in the collection. For instance:
1086+
1087+
```python
1088+
paragraphs.html = "That's cool :)"
1089+
```
10651090

10661091

10671092
## Workers

0 commit comments

Comments
 (0)