{"record":{"id":"3575f8e4ec86e20b","repo":"leptos-rs/leptos","slug":"incorrect-element-type","errorCode":null,"errorMessage":"incorrect element type","messagePattern":"incorrect element type","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tachys/src/renderer/dom.rs","lineNumber":364,"sourceCode":"                    ),\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\n        GLOBAL_EVENTS.with_borrow_mut(|events| {\n            if !events.contains(&name) {","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/leptos-rs/leptos/blob/32d20f6c9d517451d77beb68d88c7716823dac41/tachys/src/renderer/dom.rs#L346-L382","documentation":"After obtaining the event target Element, event_target casts it to the requested type T via T::cast_from and panics with 'incorrect element type' when the target isn't of type T. The library throws it because the caller statically requested a specific element type the runtime event didn't produce.","triggerScenarios":"Calling event_target::<T> with a T that doesn't match the actual target, e.g. handler on a parent (delegated listener) where the clicked child is a different tag; button handler receiving clicks on an embedded <svg> or <img>; using event_target inside a document-level listener.","commonSituations":"Event delegation where bubbling targets differ from the listened element; mixing typed handlers with delegated events; clicks landing on child nodes (text/icons) whose element type differs from the expected one.","solutions":["Verify the requested type matches the element the listener is attached to (and delegation assumptions)","Check ev.target() manually and cast with T::cast_from, handling None instead of panicking","Prefer ev.current_target()/current_target_element when you want the element the listener was registered on","Scope delegated handlers so only expected child element types can trigger them"],"exampleFix":"// before\nlet btn: HtmlButtonElement = renderer.event_target(&ev); // panics if target is <svg>\n// after\nlet t = ev.current_target().expect(\"listener element\");\nlet btn = HtmlButtonElement::cast_from(t.unchecked_into());\nif btn.is_none() { return; } // ignore unexpected targets\n","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"fn target_matches<T: CastFrom<Element>>(ev: &web_sys::Event) -> Option<T> {\n    ev.target().and_then(|t| T::cast_from(t.unchecked_into::<Element>()))\n}","tryCatchPattern":"// use the guard instead of the panicking helper:\nif let Some(el) = target_matches::<MyEl>(&ev) { /* handle */ }\n// else: ignore or handle unexpected target type","preventionTips":["Match the requested element type to the listener's actual element","For delegated listeners, cast the target and bail out on mismatch","Use current_target_element for container-level handlers","Add tests covering child-element bubbling (svg/img children)"],"tags":["dom","events","type-cast","panic"],"backgroundTag":"dom-cast-type-mismatch","analyzedSha":"32d20f6c9d517451d77beb68d88c7716823dac41","analyzedAt":"2026-09-01T18:55:42.580Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T01:17:15.007Z"}