{"record":{"id":"bf31eda6fd372270","repo":"yewstack/yew","slug":"i-m-sure-this-event-has-a-target-bf31ed","errorCode":null,"errorMessage":"I'm sure this event has a target!","messagePattern":"I'm sure this event has a target!","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"website/versioned_docs/version-0.22/concepts/basic-web-technologies/wasm-bindgen.mdx","lineNumber":146,"sourceCode":"`JsCast` provides both checked and unchecked methods of casting - so if at runtime if you are\nunsure what type a certain object is, you can try to cast it, which returns possible failure types like\n[`Option`](https://doc.rust-lang.org/std/option/enum.Option.html) and\n[`Result`](https://doc.rust-lang.org/std/result/enum.Result.html).\n\nA common example of this in [`web-sys`](./web-sys.mdx) is when you are trying to get the\ntarget of an event. You might know what the target element is, but the\n[`web_sys::Event`](https://wasm-bindgen.github.io/wasm-bindgen/api/web_sys/struct.Event.html) API will always return an [`Option<web_sys::EventTarget>`](https://wasm-bindgen.github.io/wasm-bindgen/api/web_sys/struct.Event.html#method.target).\nYou will need to cast it to the element type so you can call its methods.\n\n```rust\n// need to import the trait.\nuse wasm_bindgen::JsCast;\nuse web_sys::{Event, EventTarget, HtmlInputElement, HtmlSelectElement};\n\nfn handle_event(event: Event) {\n    let target: EventTarget = event\n        .target()\n        .expect(\"I'm sure this event has a target!\");\n\n    // maybe the target is a select element?\n    if let Some(select_element) = target.dyn_ref::<HtmlSelectElement>() {\n        // do something amazing here\n        return;\n    }\n\n    // if it wasn't a select element then I KNOW it's a input element!\n    let input_element: HtmlInputElement = target.unchecked_into();\n}\n```\n\nThe [`dyn_ref`](https://wasm-bindgen.github.io/wasm-bindgen/api/wasm_bindgen/trait.JsCast.html#method.dyn_ref)\nmethod is a checked cast that returns an `Option<&T>`, which means the original type\ncan be used again if the cast failed and thus returned `None`. The\n[`dyn_into`](https://wasm-bindgen.github.io/wasm-bindgen/api/wasm_bindgen/trait.JsCast.html#method.dyn_into)\nmethod will consume `self`, as per convention for `into` methods in Rust, and the type returned is\n`Result<T, Self>`. If the casting fails, the original `Self` value is returned in `Err`. You can try again","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/yewstack/yew/blob/0e4a05472fac4e5fce1befe60fa4a1e43a36b6a3/website/versioned_docs/version-0.22/concepts/basic-web-technologies/wasm-bindgen.mdx#L128-L164","documentation":"This panic is the `.expect()` on `Event::target()` in the raw wasm-bindgen guide's `handle_event` (the setup for the `dyn_ref::<HtmlSelectElement>()` cast demonstration). `Event::target()` is an Option because the DOM allows targetless events — events built with `Event::new` and never dispatched, or dispatched on `window`/`document` in some synthetic flows. When the handler receives such an event, the expect panics before the casts are even attempted.","triggerScenarios":"Calling `handle_event` with an `Event` constructed via `Event::new(\"change\", &Object::new()).unwrap()` (target is None until dispatched); routing targetless synthetic events from a test harness or an event-bus library into this function.","commonSituations":"Copy-pasting the wasm-bindgen casting example into a real event listener or test; integrating with libraries that re-dispatch or forward `Event` objects; headless tests constructing events manually because no DOM interaction framework is set up.","solutions":["Handle the Option before casting: `let Some(target) = event.target() else { return; };`","Keep the guide's `dyn_ref` casting pattern but drive it from the unwrapped target inside `if let` / `match` arms","When you control dispatch, dispatch synthetic events from a concrete element so the target is set"],"exampleFix":"// before\nlet target: EventTarget = event\n    .target()\n    .expect(\"I'm sure this event has a target!\");\n\n// after\nlet Some(target) = event.target() else {\n    return; // targetless event (e.g. synthetic) — nothing to inspect\n};\nif let Some(select_element) = target.dyn_ref::<HtmlSelectElement>() {\n    // handle select target\n    return;\n}","handlingStrategy":"type-guard","validationCode":"fn handle_event(event: Event) {\n    let Some(target) = event.target() else {\n        return; // targetless (synthetic) event — nothing to inspect\n    };\n    // safe to attempt type-specific handling on `target`\n}","typeGuard":"use wasm_bindgen::JsCast;\nuse web_sys::{EventTarget, HtmlSelectElement};\n\nfn as_select(t: &EventTarget) -> Option<&HtmlSelectElement> {\n    t.dyn_ref::<HtmlSelectElement>()\n}","tryCatchPattern":null,"preventionTips":["Never call expect() on Event::target(); treat Option as the normal case","Prefer dyn_ref over dyn_into when you only need to read fields — it cannot fail loudly","When synthesizing events in tests, always dispatch them from a concrete element so target is set"],"tags":["wasm-bindgen","web-sys","events","option-expect","panic"],"backgroundTag":"event-target-missing","analyzedSha":"0e4a05472fac4e5fce1befe60fa4a1e43a36b6a3","analyzedAt":"2026-08-22T21:16:31.212Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}