{"record":{"id":"6a4f911583c2bd14","repo":"leptos-rs/leptos","slug":"event-target-not-found","errorCode":null,"errorMessage":"event.target not found","messagePattern":"event\\.target not found","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tachys/src/renderer/dom.rs","lineNumber":362,"sourceCode":"                        cb.as_ref().unchecked_ref(),\n                        true\n                    ),\n                    &el,\n                    \"removeEventListener\"\n                )\n            });\n            move || cb()\n        })\n    }\n\n    pub fn event_target<T>(ev: &Event) -> T\n    where\n        T: CastFrom<Element>,\n    {\n        let el = ev\n            .unchecked_ref::<web_sys::Event>()\n            .target()\n            .expect(\"event.target not found\")\n            .unchecked_into::<Element>();\n        T::cast_from(el).expect(\"incorrect element type\")\n    }\n\n    pub fn add_event_listener_delegated(\n        el: &Element,\n        name: Cow<'static, str>,\n        delegation_key: Cow<'static, str>,\n        cb: Box<dyn FnMut(Event)>,\n    ) -> RemoveEventHandler<Element> {\n        let cb = Closure::wrap(cb);\n        let key = intern(&delegation_key);\n        or_debug!(\n            js_sys::Reflect::set(el, &JsValue::from_str(key), cb.as_ref()),\n            el,\n            \"set property\"\n        );\n","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/leptos-rs/leptos/blob/32d20f6c9d517451d77beb68d88c7716823dac41/tachys/src/renderer/dom.rs#L344-L380","documentation":"Renderer::event_target reads ev.target() off the underlying web_sys::Event and panics if it is None. Per DOM spec target is usually present, but for events that have not been dispatched or synthetic/manually constructed events it can be missing, so the library panics to surface the invalid event.","triggerScenarios":"Calling event_target with an event whose target is None: manually constructed/dispatched events without a target, events fetched after dispatch teardown, or passing a non-Event object that unchecked_ref misinterprets.","commonSituations":"Unit tests invoking handlers with fabricated Event objects; custom event delegation code reusing stale events; calling event_target outside an actual listener callback.","solutions":["Only call event_target inside a real listener callback with a dispatched event","Check ev.target() yourself and use a fallback (e.g. current_target) when None","In tests, create the event via dispatched dispatch_event on a real element","For delegated listeners, prefer accessing the event target through the delegation machinery provided by the renderer"],"exampleFix":"// before\nlet el: HtmlElement<MyEl> = renderer.event_target::<MyEl>(&synthetic_event); // panics\n// after\nif let Some(t) = synthetic_event.target() {\n    let el = MyEl::cast_from(t.unchecked_into()).unwrap_or_default();\n}\n","handlingStrategy":"validation","validationCode":"fn event_has_target(ev: &web_sys::Event) -> bool { ev.target().is_some() }\n// only call event_target for dispatched events inside live listeners","typeGuard":"fn target_of(ev: &web_sys::Event) -> Option<web_sys::Element> {\n    ev.target().map(|t| t.unchecked_into::<web_sys::Element>())\n}","tryCatchPattern":"// pre-check instead of catching the panic:\nif let Some(t) = ev.target() {\n    let el = t.unchecked_into::<Element>();\n    // proceed with cast_from\n}","preventionTips":["Only call event_target inside real listener callbacks","In tests, dispatch events with dispatch_event on real elements rather than fabricating them","Prefer current_target when the handler is on a container","Guard delegated handlers for events that may have detached targets"],"tags":["dom","events","panic"],"backgroundTag":"event-target-missing","analyzedSha":"32d20f6c9d517451d77beb68d88c7716823dac41","analyzedAt":"2026-09-01T18:55:42.580Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T01:17:15.007Z"}