Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1662

Blur Child Input Component from Parent in Svelte 5

$
0
0

I have a custom <MyInput /> component like this:

<script lang="ts">//=== MyInput.svelte ===type Props = {  input: HTMLInputElement  value: string}let { input = $bindable(), value = $bindable() }: Props = $props()</script><input bind:this={input} bind:value />

I want to be able to programmatically blur this field from a parent component wherever it is used.

<script lang="ts">//=== Parent.svelte ===import MyInput from '$lib/MyInput.svelte'let input: HTMLInputElementfunction blurField(){  input.blur() <!-- console error}</script><MyInput {input} {value} /><button onclick={() => blurField()}>Blur Field</button>

I'm unclear on how to bind my reference to the input in the parent to the actual child component. It doesn't appear to actually have connected the input in the parent to the binding of the child.

Any ideas what I'm doing wrong?


Viewing all articles
Browse latest Browse all 1662

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>