You need to use one of the 6 "magic properties", in your case the $event
one:
window.MyComponent = () => ({
value: 0,
download(e) {
this.value = e.currentTarget.dataset.url;
},
});
body {
font-family: monospace;
}
.button {
display: inline-flex;
border: 2px solid black;
min-width: 64px;
min-height: 72px;
border-radius: 10px;
font-family: monospace;
cursor: pointer;
align-items: center;
justify-content: center;
}
.button:hover {
background: yellow;
}
.nestedSpan {
display: inline-block;
border: 1px dotted black;
padding: 8px;
border-radius: 8px;
}
.nestedSpan:hover {
background: orange;
}
.nestedSpan > .nestedSpan {
border-radius: 4px;
}
.nestedSpan > .nestedSpan:hover {
background: red;
}
<div x-data="MyComponent()">
<a
@click.prevent="download($event)"
data-video="video_id"
data-url="{{ base_url('tools/download_thumbnail_A') }}"
data-quality="maxres"
class="button">
A
</a>
<a
@click.prevent="download($event)"
data-video="video_id"
data-url="{{ base_url('tools/download_thumbnail_B') }}"
data-quality="maxres"
class="button">
<span class="nestedSpan">B</span>
</a>
<a
@click.prevent="download($event)"
data-video="video_id"
data-url="{{ base_url('tools/download_thumbnail_C') }}"
data-quality="maxres"
class="button">
<span class="nestedSpan">
<span class="nestedSpan">C</span>
</span>
</a>
<p x-text="value"></p>
</div>
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js"></script>
Note that in the event handler I'm still calling it e
, but in the HTML I've used download($event)
instead of download(e)
.
Also, note how I'm using e.currentTarget
instead of e.target
, as the former will reference the element the click event listener was added to, while the latter references the element that triggered the event (the actual element the user clicks, which might be a child of the element the even was added to), so e.target
only works as expected if you have no children elements.