How to get an HtmlTextAreaElement in web_sys?

Hi there,

I'm trying to understand web_sys, but I got stuck. For some html-page with

        <textarea id="your_message"></textarea>

I want to get the data of the texatarea. I can get the Element:

        let window = web_sys::window().expect("no global `window` exists");
        let document = window.document().expect("should have a document on window");
        let your_message = document.get_element_by_id("your_message").unwrap();

But now I'm stuck. If I use your_message.value_of(), I get an Object, and I don't know how to handle that. In the ideal case, I would like to get an HtmlTextAreaElement, and then use value() on that. Does anybody know what I'm missing here?

Thanks in advance,

Linus

From all places, Reddit got an answer - but I don't know if this is really the best way to do it.

        let your_message = document
            .get_element_by_id("your_message")
            .unwrap()
            .dyn_into::<HtmlTextAreaElement>()
            .unwrap();

Yes, that's how you convert the static type when needed. Note that the unwrap() will panic if the element isn't a textarea.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.