[Web] How to modify <html> tag attributes programmatically? #4162
-
Hi. I could not find anything in the documentation or examples online, but is there a programmatic way to set the <html> tag(s)? I could do something like this example: https://github.com/DioxusLabs/dioxus/blob/main/examples/custom_html.rs It would be nice if there were something like document::Html {...} to set the tags directly. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello! To modify the attributes of the use web_sys::{wasm_bindgen::JsCast, HtmlElement};
let html_element = web_sys::window()
.expect("Window not found")
.document()
.expect("Document not found")
.document_element()
.expect("Document element not found")
.dyn_into::<HtmlElement>()
.expect("Failed to cast to HtmlElement"); Once you have the html_element
.set_attribute("data-theme", "dark")
.expect("Failed to set attribute"); I learned this approach while building Note: When using web-sys = { version = "0.3.77", features = [
"Document",
"Window",
"HtmlHeadElement",
]} Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hello!
To modify the attributes of the
<html>
tag, you can use theweb-sys
crate to retrieve thedocument_element
and downcast it to anHtmlElement
as follows:Once you have the
HtmlElement
, you can set an attribute like this:I learned this approach while building
theme
.Note: When using
w…