Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove `href` attribute from link with Svelte? Should this be enough?

Am I wrong or this should remove href attribute from the a tag?

<a href={false}></a>

It doesn't ("svelte": "3.44.2").

like image 809
Fred Hors Avatar asked Sep 06 '25 03:09

Fred Hors


2 Answers

The docs say

Boolean attributes are included on the element if their value is truthy and excluded if it's falsy.
All other attributes are included unless their value is nullish (null or undefined).

href= is no boolean attribute, so false doesn't work, use null/undefined instead

<a href={null}>linkText</a>
<a href={undefined} >linkText</a>
like image 77
Corrl Avatar answered Sep 07 '25 22:09

Corrl


Use null and it will deactivate the hyperlink and won't go anywhere. Or you can add on:click|preventDefault to the anchor.

<a href={null} on:click|preventDefault>link</a>
like image 24
Sherif Salah Avatar answered Sep 07 '25 23:09

Sherif Salah