Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a checkbox or radio button is checked in Dart?

I have a checkbox and a radio button group and I want to know if the checkbox is checked and which radio button is selected.

How do I do this in dart?

like image 675
Eduardo Copat Avatar asked Nov 01 '12 17:11

Eduardo Copat


1 Answers

Let's say we have your HTML with something like this:

    <form >
      <input type="radio" name="gender" id="gender_male" value="male">Male<br>
      <input type="radio" name="gender" id="gender_female" value="female">Female
    </form>

    <form>
      <input type="checkbox" id="baconLover">I like bacon<br>
    </form>

Your Dart code to obtain their values would be something like the following, I also added an event to know when the checkbox is clicked.

import 'dart:html';

void main() {

  // Adds a click event when the checkbox is clicked
  query("#baconLover").on.click.add((MouseEvent evt) {
    InputElement baconCheckbox = evt.target;

    if (baconCheckbox.checked) {
      print("The user likes bacon");
    } else {
      print("The user does not like bacon");
    }

  });

  // Adds a click event for each radio button in the group with name "gender"
  queryAll('[name="gender"]').forEach((InputElement radioButton) {
    radioButton.onclick.listen((e) {
      InputElement clicked = e.target;
      print("The user is ${clicked.value}");
    });
  });

}
like image 91
Eduardo Copat Avatar answered Nov 03 '22 00:11

Eduardo Copat