Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get form input value with Dart

I'm trying to get the value of an input element e.g. this is simple form:

<form id="loginForm">
        <label for="username">Username</label>
        <input required type="text" class="form-control" id="username">
        <label for="password">Passowrd</label>
        <input required type="password"id="password">
    </div>
    <button type="submit">Submit</button>
</form>

with jQuery I would have written:

let value = $("#password").val();

or I could even serialize the whole form,

but in Dart this seems not working:

querySelector('#username').getAttribute('value');

, it returns null

I'm not using any frameworks,

any suggestions?

like image 715
Mattia Avatar asked Apr 26 '26 21:04

Mattia


1 Answers

querySelector will only return an HtmlElement. Since you know the result is an InputElement (a TextInputElement at that) you need to cast it to gain access to the value attribute.

(querySelector('#usernames') as InputElement).value

like image 161
DomJack Avatar answered Apr 29 '26 22:04

DomJack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!