{"record":{"id":"d4218c6def823f0d","repo":"yewstack/yew","slug":"i-m-sure-this-event-has-a-target-d4218c","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.23/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.23/concepts/basic-web-technologies/wasm-bindgen.mdx#L128-L164","documentation":"This panic is the `.expect(\"I'm sure this event has a target!\")` on `Event::target()` in Yew 0.23's wasm-bindgen guide (`handle_event`). `Event::target()` is an Option because events are not required to have a target — `Event::new` produces one with `target === null` until it is dispatched on a node. The guide uses expect for brevity before demonstrating `dyn_ref` casts; any targetless event reaching this function panics.","triggerScenarios":"Feeding `handle_event` a synthesized `Event` (built with `Event::new`, never dispatched); forwarding events from event-bus or test utilities that construct bare events; dispatch flows where the target has been cleared.","commonSituations":"Copy-pasting the casting example from wasm-bindgen.mdx into real listeners or unit tests; integrating with libraries that re-dispatch forwarded events.","solutions":["Handle the Option: `let Some(target) = event.target() else { return; };`","Keep the `dyn_ref::<HtmlSelectElement>()` / `dyn_ref::<HtmlInputElement>()` branching but under the unwrapped target","Dispatch synthetic events from a concrete element when you control the test"],"exampleFix":"// before\nlet target: EventTarget = event\n    .target()\n    .expect(\"I'm sure this event has a target!\");\n\n// after\nlet Some(target: EventTarget) = event.target() else {\n    return;\n};\nif let Some(select_element) = target.dyn_ref::<HtmlSelectElement>() {\n    // do something amazing here\n    return;\n}","handlingStrategy":"type-guard","validationCode":"fn handle_event(event: Event) {\n    let Some(target) = event.target() else {\n        return;\n    };\n    let _ = 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":["Treat Event::target() as Optional by default; skip targetless events","Prefer dyn_ref for read-only casts; it removes the Result/expect entirely","Synthesize test events by dispatching from a real element"],"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"}