Your target selectors may use this pseudo-selector to reference the origin element that triggered the change.
The :origin placeholder will be replaced with a target derived
from the origin element.
Below we see two links that will each update the <div> next to them.
This requires a rather verbose [up-target] attribute:
<a href="/tasks/1" up-target="a[href='/tasks/1'] + div">Show task 1</a> <!-- mark: a[href='/tasks/1'] + div -->
<div>Task 1 will appear here</div
<a href="/tasks/2" up-target="a[href='/tasks/2'] + div">Show task 2</a> <!-- mark: a[href='/tasks/2'] + div -->
<div>Task 2 will appear here</div
We can simplify the [up-target] by referencing the followed link by :origin:
<a href="/tasks/1" up-target=":origin + div">Show task 1</a> <!-- mark: :origin + div -->
<div>Task 1 will appear here</div
<a href="/tasks/2" up-target=":origin + div">Show task 2</a> <!-- mark: :origin + div -->
<div>Task 2 will appear here</div
When a link is clicked, :origin with a target derived from the link element.
For example, clicking on the second link will target a[href='/tasks/2'].
The origin element is automatically set for many actions, for example:
| Action | Origin element |
|---|---|
| Submitting a form with submit button | The submit button |
Submitting a form by pressing Return
|
The focused field |
| Following a link | The link |
| Preloading a link | The link |
| Validating a field | The changed field |
| Auto-submitting a field | The changed field |
| Loading deferred content | The [up-defer] placeholder |
When updating fragments programmatically through functions like up.render()
you may pass an origin element as an { origin } option:
element.addEventListener('click', function(event) {
up.render('.preview', { origin: element })
})
You do not need to pass an { origin } for functions that already have a
natural origin:
up.follow(link) // link will be set as { origin }
Tip
Ensuring an origin is set may improve the precision of fragment lookup, even if a target selector doesn't contain an
:originreference. In the example above, Unpoly would prefer to match.previewin the region of the origin. If no origin is known, Unpoly will always match the first.previewin the current layer.