web_sys::Document::query_selector function

Example

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");
	}

Function


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

features/dependencies

Source Code