get value of selected <select> element in dart
Question
how i can obtain selected option of element? document.query("#id") - returns Element which hasn't value propery so i can't do:
var val = document.query("#id").value;
var val = (SelectElement)(document.query("#id")).value; // - "SelectElement is class and cannot be used as an expression"
how to do type conversion in dart correctly?
Solution 2
just need:
String str = document.query("#idOfSelect").value;
will be warning, but you can ignore it
OTHER TIPS
In general, when dealing with any HTML element, I usually query the HTML element first:
InputElement myElement = document.query(#myInputElement);
Then use some event to capture the value contained in that element:
int val = Math.parseInt(myElement.value);
So for example, in HTML I could have an input element of type range:
<input id="mySlider" type="range" min="1000" max="10000" step="20" value="5000" />
In Dart, I would grab the value of this slider using a mouse up event:
InputElement mySlider = document.query('#mySlider');
mySlider.on.mouseUp.add((e) {
int _val = Math.parseInt(mySlider.value);
document.query('#status').innerHTML = "$_val";
});
Edit: In M1, you can now just use the cast operator as
. If your HTML looked like this:
<p id="text">This is some text</p>
You could retrieve the text from the paragraph tag by doing this:
String str = (query('#text') as ParagraphElement).text;
I used:
var val = int.parse(query("#id").value, onError: (_) => 1);
This prevents an exception in case query("#id").value is null and assigns a default value (1).