This example demonstrates the use of document.query_selector to select a <p> element. If there are multiple <p> element, the first one gets selected.
The method returns a Result<Option<Element>, JsValue>, thus requiring multiple unwrap in the code below. After getting the HtmlElement, the set_inner_text method is called to set a "Hello..." message.
pub fn document_query_selector()
{
let window = web_sys::window().expect("global window does not exists");
let document = window.document().expect("expecting a document on window");
let val = document.query_selector("p")
.unwrap()
.unwrap()
.dyn_into::<web_sys::HtmlElement>()
.unwrap();
val.set_inner_text(&"Hello from querySelector");
}
pub fn query_selector(
&self,
selectors: &str
) -> Result<Option, JsValue>