Skip to main content

ElementExt

Trait ElementExt 

Source
pub trait ElementExt {
Show 16 methods // Required methods fn set_class(&self, class: &str); fn set_id(&self, id: &str); fn set_text(&self, text: &str); fn set_html(&self, html: &str) -> Result<(), JsValue>; fn set_attr(&self, attr: &str, value: &str) -> Result<(), JsValue>; fn get_attr(&self, attr: &str) -> Option<String>; fn remove_attr(&self, attr: &str) -> Result<(), JsValue>; fn add_class(&self, class: &str) -> Result<(), JsValue>; fn remove_class(&self, class: &str) -> Result<(), JsValue>; fn toggle_class(&self, class: &str) -> Result<(), JsValue>; fn has_class(&self, class: &str) -> bool; fn on<F>(&self, event: &str, handler: F) -> Result<(), JsValue> where F: FnMut(Event) + 'static; fn append(&self, child: &Element) -> Result<(), JsValue>; fn prepend(&self, child: &Element) -> Result<(), JsValue>; fn remove(&self) -> Result<(), JsValue>; fn clear(&self);
}
Expand description

Ergonomic extension methods for web_sys::Element.

Provides concise helpers for common DOM manipulations (attributes, classes, text/HTML content, event listeners, and child insertion/removal) on top of the lower-level web-sys API.

Required Methods§

Source

fn set_class(&self, class: &str)

Sets the class attribute, replacing any existing class list.

Source

fn set_id(&self, id: &str)

Sets the id attribute.

Source

fn set_text(&self, text: &str)

Replaces the element’s text content with text.

Source

fn set_html(&self, html: &str) -> Result<(), JsValue>

Replaces the element’s inner HTML with html.

Source

fn set_attr(&self, attr: &str, value: &str) -> Result<(), JsValue>

Sets attribute attr to value.

Source

fn get_attr(&self, attr: &str) -> Option<String>

Returns the value of attribute attr, or None if it is absent.

Source

fn remove_attr(&self, attr: &str) -> Result<(), JsValue>

Removes attribute attr.

Source

fn add_class(&self, class: &str) -> Result<(), JsValue>

Adds class to the class attribute if it is not already present.

Source

fn remove_class(&self, class: &str) -> Result<(), JsValue>

Removes class from the class attribute, dropping the attribute entirely when no classes remain.

Source

fn toggle_class(&self, class: &str) -> Result<(), JsValue>

Adds class if absent, or removes it if present.

Source

fn has_class(&self, class: &str) -> bool

Returns true if the class attribute contains class as a whole token.

Source

fn on<F>(&self, event: &str, handler: F) -> Result<(), JsValue>
where F: FnMut(Event) + 'static,

Attaches handler as a listener for event.

The underlying closure is leaked so the listener remains valid for the lifetime of the element.

Source

fn append(&self, child: &Element) -> Result<(), JsValue>

Appends child as the last child of this element.

Source

fn prepend(&self, child: &Element) -> Result<(), JsValue>

Inserts child as the first child of this element, or appends it when the element has no children.

Source

fn remove(&self) -> Result<(), JsValue>

Detaches this element from its parent; a no-op when it has no parent.

Source

fn clear(&self)

Removes all child nodes of this element.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ElementExt for Element

Source§

fn set_class(&self, class: &str)

Source§

fn set_id(&self, id: &str)

Source§

fn set_text(&self, text: &str)

Source§

fn set_html(&self, html: &str) -> Result<(), JsValue>

Source§

fn set_attr(&self, attr: &str, value: &str) -> Result<(), JsValue>

Source§

fn get_attr(&self, attr: &str) -> Option<String>

Source§

fn remove_attr(&self, attr: &str) -> Result<(), JsValue>

Source§

fn add_class(&self, class: &str) -> Result<(), JsValue>

Source§

fn remove_class(&self, class: &str) -> Result<(), JsValue>

Source§

fn toggle_class(&self, class: &str) -> Result<(), JsValue>

Source§

fn has_class(&self, class: &str) -> bool

Source§

fn on<F>(&self, event: &str, handler: F) -> Result<(), JsValue>
where F: FnMut(Event) + 'static,

Source§

fn append(&self, child: &Element) -> Result<(), JsValue>

Source§

fn prepend(&self, child: &Element) -> Result<(), JsValue>

Source§

fn remove(&self) -> Result<(), JsValue>

Source§

fn clear(&self)

Implementors§