Skip to content

Clearable form creation #983

Answered by gbj
amfaber asked this question in Q&A
May 3, 2023 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

If I understand correctly, the question here is basically "How do I get access to the HtmlFormElement from inside on_submit?"

The simplest way to do this is to use ev.target() and cast the type of it from web_sys::EventTarget to web_sys::HtmlFormElement (using the wasm_bindgen::JsCast trait)

let on_submit = move |ev: SubmitEvent|{
  let form = ev.target().and_then(|target| target.dyn_into::<HtmlFormElement>());
  if let Some(form) = form {
    // aggregate information from the form with signals
    form.reset();
  } else {
    leptos::error!("huh?");
  }
};

Another way to do this would be to use let form_ref = create_node_ref::<html::Form>(cx) to hold a reference to the form, and load it …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@amfaber
Comment options

Answer selected by amfaber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants