The svelte:element tag lets you create HTML elements dynamically. This means you can choose which tag to use while your app runs, not just when you write the code.
svelte:element for dynamic tags
<svelte:element this={tagName} {...props}>Content</svelte:element>The this attribute sets which HTML tag to render.
You can pass other attributes or event handlers as usual.
<button> with text 'Click me'.<script> let tagName = 'button'; </script> <svelte:element this={tagName}>Click me</svelte:element>
<a>) that opens in a new tab.<script> let tagName = 'a'; let href = 'https://example.com'; </script> <svelte:element this={tagName} href={href} target="_blank">Visit</svelte:element>
<section> with accessible label and nested content.<script> let tagName = 'section'; </script> <svelte:element this={tagName} aria-label="Main section"> <p>Section content</p> </svelte:element>
This example shows a box that changes its HTML tag between div and button when clicked. The text updates to show the current tag. The style and click event work on both tags.
<script> import { onMount } from 'svelte'; let tag = 'div'; let count = 0; function toggleTag() { tag = tag === 'div' ? 'button' : 'div'; } onMount(() => { console.log(`Initial tag is: ${tag}`); }); </script> <svelte:element this={tag} on:click={toggleTag} style="cursor: pointer; padding: 1rem; border: 1px solid #ccc; display: inline-block;"> Click me to toggle tag (currently: {tag}) </svelte:element>
Remember to use valid HTML tags in the this attribute to avoid errors.
You can pass any attributes or event handlers to svelte:element just like normal elements.
Using svelte:element helps keep your components flexible and reusable.
svelte:element lets you pick the HTML tag dynamically.
Use the this attribute to set the tag name.
This helps make flexible and reusable components without repeating code.
Practice
svelte:element tag in Svelte?Solution
Step 1: Understand
svelte:elementusagesvelte:elementallows you to specify which HTML tag to render dynamically using thethisattribute.Step 2: Compare options
Only To render an HTML element with a tag name set dynamically at runtime. correctly describes this dynamic tag rendering feature. Other options describe unrelated features.Final Answer:
To render an HTML element with a tag name set dynamically at runtime. -> Option CQuick Check:
svelte:element = dynamic tag rendering [OK]
this sets the tag name dynamically [OK]- Thinking
svelte:elementcreates components - Confusing it with data binding
- Assuming it imports styles
svelte:element with a variable tagName?Solution
Step 1: Recall the attribute for dynamic tag
Thethisattribute is used to specify the tag name dynamically insvelte:element.Step 2: Check each option
Only<svelte:element this={tagName} />usesthis={tagName}, which is the correct syntax. Others use incorrect attribute names.Final Answer:
<svelte:element this={tagName} /> -> Option AQuick Check:
Dynamic tag attribute = this [OK]
this attribute for dynamic tags [OK]- Using
tagornameinstead ofthis - Forgetting curly braces around variable
- Using invalid attribute names
<script>
let tag = 'section';
</script>
<svelte:element this={tag} class="box">Hello</svelte:element>What HTML will be rendered in the browser?
Solution
Step 1: Identify the dynamic tag value
The variabletagis set to'section'.Step 2: Understand
Thesvelte:elementrenderingsvelte:elementrenders the HTML tag specified bythis={tag}, so it renders a<section>element with class "box" and content "Hello".Final Answer:
<section class="box">Hello</section> -> Option BQuick Check:
Dynamic tag = section, renders <section> [OK]
this [OK]- Assuming default tag like <div>
- Thinking
svelte:elementrenders literally - Ignoring the dynamic
thisvalue
<script>
let tag = 'article';
</script>
<svelte:element this=tag>Content</svelte:element>Solution
Step 1: Check attribute binding syntax
In Svelte, to bind a variable to an attribute, you must use curly braces. Here,this=tagshould bethis={tag}.Step 2: Verify other options
The tag name 'article' is not a valid HTML tag. is false because 'article' is a valid HTML tag. You cannot usesvelte:elementwith variables. is false;svelte:elementsupports variables. The closing tag is missing. is false; the closing tag is present.Final Answer:
Missing curly braces around the variabletaginthisattribute. -> Option DQuick Check:
Variable binding needs curly braces [OK]
- Forgetting curly braces for dynamic attributes
- Thinking tag names must be strings only
- Assuming
svelte:elementcan't use variables
tag. Which approach correctly uses svelte:element to achieve this, including passing down all other attributes and content?Solution
Step 1: Identify correct dynamic tag usage
To render a dynamic HTML tag,svelte:elementwiththis={tag}is the correct pattern.Step 2: Check other options for errors
<script> export let tag = 'div'; </script> <{tag} class="container"> <slot /> </{tag}>uses invalid syntax with <{tag}>.<script> export let tag = 'div'; </script> <div this={tag} class="container"> <slot /> </div>incorrectly usesthison a fixed <div>.<script> export let tag = 'div'; </script> <svelte:component this={tag} class="container"> <slot /> </svelte:component>usessvelte:component, which is for dynamic Svelte components, not HTML tags.Final Answer:
Option A correctly usessvelte:elementwiththis={tag}and a slot for content. -> Option AQuick Check:
Dynamic HTML tags use svelte:element + this [OK]
svelte:element this={tag} for dynamic HTML tags [OK]- Using
svelte:componentfor HTML tags - Trying to use variable tags with normal HTML syntax
- Misplacing
thisattribute on fixed tags
Ready to prove it?
Pass the test and this pattern moves unknown
