web_sys::Document::query_selector function


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")
		val.set_inner_text(&"Hello from querySelector");


	pub fn query_selector(
		selectors: &str
	) -> Result<Option, JsValue>


Source Code