uploadcare / uploadcare-widget

Uploadcare Widget, an ultimate tool for HTML5 file upload supporting multiple file upload, drag&drop, validation by file size/file extension/MIME file type, progress bar for file uploads, image preview.

Home Page:https://uploadcare.com/products/file_uploader/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No DOM elements found matching selector in Svelte

hauselin opened this issue · comments

Summary

When using the widget with Svelte, I get this error: No DOM elements found matching selector. Svelte code is included below. I really appreciate any help anyone can provide. Thanks!

<script>
	import uploadcare from "uploadcare-widget/uploadcare.lang.en.min.js";
	
	const widget = uploadcare.Widget("#uploader", {
		publicKey: "demopublickey",
		imagesOnly: true,
		crop: "300x200",
	});
</script>

<input id="uploader" type="hidden" />

Relevant information

See Svelte REPL/example here.

Environment (if relevant)

Svelte 3.44.3

Hi @hauselin, you can use this binding to get a reference to a rendered element. See https://svelte.dev/tutorial/bind-this. Here's how you can solve this:

<script>
	import uploadcare from "uploadcare-widget/uploadcare.lang.en.min.js";
	import { onMount } from "svelte";
	
	let widgetInput, widget;
	
	onMount(() => {
		widget = uploadcare.Widget(widgetInput, {
			publicKey: "demopublickey",
			imagesOnly: true,
			crop: "300x200",
		});
		console.log(widget);
	});
</script>

<input id="uploader" type="hidden" bind:this={widgetInput} />

Hope this helps.

Perfect! I've updated the code and it works. Thanks also for the reference to Svelte's bind:this. Thanks, @optlsnd!