yewstack/yew · error
element with id `mousemoveme` not present
Error message
element with id `mousemoveme` not present
What it means
This panic is `.expect("element with id `mousemoveme` not present")` after `get_element_by_id("mousemoveme")` in the web-sys guide. `Document::get_element_by_id` returns `Option<Element>`, None when no element in that document carries the exact id. The guide's example only works if the served page markup contains an element with `id="mousemoveme"`; it also fails permanently when the lookup ran on the blank document produced by `Document::new()` (error 66), because a freshly constructed document has no elements at all.
Source
Thrown at website/versioned_docs/version-0.22/concepts/basic-web-technologies/web-sys.mdx:184
use wasm_bindgen::{prelude::Closure, JsCast};
use web_sys::{console, Document, HtmlElement, MouseEvent};
let mousemove = Closure::<dyn Fn(MouseEvent)>::wrap(Box::new(|e| {
let rect = e
.target()
.expect("mouse event doesn't have a target")
.dyn_into::<HtmlElement>()
.expect("event target should be of type HtmlElement")
.get_bounding_client_rect();
let x = (e.client_x() as f64) - rect.left();
let y = (e.client_y() as f64) - rect.top();
console::log_1(&format!("Left? : {} ; Top? : {}", x, y).into());
}));
Document::new()
.expect("global document not set")
.get_element_by_id("mousemoveme")
.expect("element with id `mousemoveme` not present")
.unchecked_into::<HtmlElement>()
.set_onmousemove(mousemove.as_ref().dyn_ref());
// we now need to save the `mousemove` Closure so that when
// this event fires the closure is still in memory.
```
This version is much more verbose, but you will probably notice part of that is because of failure
types reminding us that some of these function calls have invariants that must be held, or otherwise will
cause a panic in Rust. Another part of the verbosity is the calls to `JsCast` to cast into
different types so that you can call its specific methods.
### Yew example
In Yew you will mostly be creating [`Callback`](concepts/function-components/callbacks.mdx)s to use in the
[`html!`](concepts/html/introduction.mdx) macro so the example is going to use this approach instead of completely copying
the approach above:
View on GitHub (pinned to 0e4a05472f)
Solutions
- Add `<div id="mousemoveme"></div>` (any element with that id) to `index.html`
- Fix the document accessor first: use `gloo::utils::document()` instead of `Document::new()` (see error 66)
- Load the script as `type="module"` or with `defer` so the DOM exists before the lookup
- Replace the expect with `if let Some(el) = ...` plus a `gloo::console::log` warning to fail soft when the element is absent
Example fix
<!-- index.html -->
<body>
<div id="mousemoveme"></div>
<script type="module" src="..."></script>
</body>
// before
Document::new()
.expect("global document not set")
.get_element_by_id("mousemoveme")
.expect("element with id `mousemoveme` not present")
// after
if let Some(el) = gloo::utils::document().get_element_by_id("mousemoveme") {
el.unchecked_into::<HtmlElement>()
.set_onmousemove(mousemove.as_ref().dyn_ref());
} else {
gloo::console::warn("#mousemoveme not present");
} Defensive patterns
Strategy: validation
Validate before calling
let Some(el) = gloo::utils::document().get_element_by_id("mousemoveme") else {
gloo::console::warn("#mousemoveme not present — mousemove not attached");
return;
};
// attach the listener only when the element exists
el.unchecked_into::<web_sys::HtmlElement>()
.set_onmousemove(mousemove.as_ref().dyn_ref()); Prevention
- Add required ids to index.html before writing the Rust that looks them up
- Keep element ids in a shared constants module used by both HTML template and Rust
- Load the wasm entry as a module script (or defer) so the DOM is parsed first
- Fail soft with a console warning for optional elements; reserve expect for hard app-shell invariants
When it happens
Trigger: `index.html` has no element with `id="mousemoveme"`; the script runs before the body is parsed (classic script in `<head>` without `defer`); the id is misspelled; or the preceding `Document::new()` returned an empty document so the lookup can never succeed.
Common situations: Copy-pasting the web-sys.mdx mousemove demo without adding the tracked element to `index.html`; wiring the example into a template whose HTML shell omits the div; running under Trunk with the script tag placed before the target element.
Related errors
- element with id `mousemoveme` not present
- global document not set
- Expected to find a #modal_host element
- I'm sure this event has a target!
- mouse event doesn't have a target
AI-assisted analysis of yewstack/yew@0e4a05472f (2026-08-22).
Data as JSON: /api/errors/e9d5efd5b6a8a7ce.
Report an issue: GitHub.