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 Yew 0.23's web-sys guide. The lookup returns `Option<Element>`, None when no element in the looked-up document has that exact id. The demo requires `<div id="mousemoveme">` in the served HTML; it also always fails if the preceding `Document::new()` (error 75) produced an empty constructed document rather than fetching the global one.

Source

Thrown at website/versioned_docs/version-0.23/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: MouseEvent| {
    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

  1. Add `<div id="mousemoveme"></div>` to `index.html`
  2. Replace `Document::new()` with `gloo::utils::document()` so you search the real page (see error 75)
  3. Load the script as a module or with `defer` so the element exists first
  4. Swap the expect for `if let Some(el)` with a console warning for a soft failure

Example fix

<!-- index.html -->
<div id="mousemoveme"></div>

// before
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());

// after
if let Some(el) = gloo::utils::document().get_element_by_id("mousemoveme") {
    el.unchecked_into::<HtmlElement>()
        .set_onmousemove(mousemove.as_ref().dyn_ref());
}
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");
    return;
};
// proceed to attach the handler only when the element exists

Prevention

When it happens

Trigger: `index.html` lacks `id="mousemoveme"`; the script executes before the target element is parsed (classic script in `<head>`); id typo; lookup performed on the blank document returned by `Document::new()`.

Common situations: Copy-pasting the 0.23 web-sys.mdx demo into a Trunk project without adding the tracked div; script-tag ordering issues; template shells that omit the element.

Related errors


AI-assisted analysis of yewstack/yew@0e4a05472f (2026-08-22). Data as JSON: /api/errors/b1f8e440a4fb3f3d. Report an issue: GitHub.