yewstack/yew · warning

element with id `mousemoveme` not present

Error message

element with id `mousemoveme` not present

What it means

get_element_by_id returns Option, and this expect fires when no element with id mousemoveme exists at call time. In the tutorial context the usual cause is ordering: the script runs (from <head>, without defer) before the element has been parsed. Otherwise it is a plain mismatch - a typo, a case difference, or an element the framework has not rendered yet.

Source

Thrown at website/versioned_docs/version-0.21/concepts/basic-web-technologies/web-sys.mdx:185

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

  1. Run the lookup after DOMContentLoaded, or move the script to the end of <body> / add defer
  2. Verify the id string matches the markup exactly (ids are case-sensitive)
  3. Handle the Option: match document.get_element_by_id(...) { Some(el) => ..., None => log }
  4. For framework-rendered elements, query from a component lifecycle hook instead of startup code

Example fix

// before
let el = gloo::utils::document()
    .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 {
    web_sys::console::warn_1(&"missing #mousemoveme".into());
}
Defensive patterns

Strategy: validation

Validate before calling

// Handle the Option instead of expecting, especially at startup:
match gloo::utils::document().get_element_by_id("mousemoveme") {
    Some(el) => attach_listener(&el),
    None => web_sys::console::warn_1(&"#mousemoveme not present (yet)".into()),
}

Prevention

When it happens

Trigger: Module-level or #[wasm_bindgen(start)] code querying the DOM during page parse; ids that differ by case or typo; elements rendered later by the framework or injected by scripts.

Common situations: Script tag in <head> without defer; copying the example without adding <div id="mousemoveme"> to the page; dynamic ids; tests with incomplete DOM fixtures.

Related errors


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