#How do i activate a click function on a anchor tag?
3 messages · Page 1 of 1 (latest)
Hi @tribal flint. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Astro does not support using JSX-style event handlers like onClick={...} directly on HTML elements such as <a> tags in your .astro files. Instead, Astro follows standard HTML and recommends adding interactivity using JavaScript in a <script> tag.
To activate a click function on an anchor (<a>) tag, you should use addEventListener inside a <script> tag. Here’s an example:
<a id="my-link" href="#">Click me!</a>
<script>
document.getElementById('my-link').addEventListener('click', (event) => {
event.preventDefault(); // Prevents the default link behavior
alert('Anchor was clicked!');
});
</script>