Fred Hors
Fred Hors

Reputation: 4146

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").

Upvotes: 4

Views: 4120

Answers (2)

Corrl
Corrl

Reputation: 7721

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>

Upvotes: 7

Sherif Salah
Sherif Salah

Reputation: 2153

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>

Upvotes: 2

Related Questions