On Create Element
The on_create_elem special attribute allows you to register a function that will be called
when the element is first created.
#![allow(unused)] fn main() { let mut div: VirtualNode = html! { <div> <span>This span should get replaced</span> </div> }; let special_attributes = &mut div.as_elem_mut().unwrap().special_attributes; special_attributes.set_key("some-key"); special_attributes .set_on_create_element(move |elem: web_sys::Element| { elem.set_inner_html("Hello world"); }) .unwrap(); let div: Element = div.create_dom_node().node.unchecked_into(); assert_eq!(div.inner_html(), "Hello world"); }
Macro shorthand
You can also use the html! macro to set the on_create_element function.
#![allow(unused)] fn main() { let _ = html! { <div key="some-key" on_create_element = move |element: web_sys::Element| { element.set_inner_html("After"); } > Before </div> } }