Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit a form in Dart using an HTML link

Tags:

html

dart

Let's say I have a form:

<form id = "form1" action = "process.php" method = "post">
...
</form>

Now, submitting it from JavaScript using link would look like this:

<a href='javascript:document.forms["form1"].submit()'>Submit</a>

How do I do the same with Dart-lang?

like image 303
Leonardo DaVintik Avatar asked Nov 08 '12 13:11

Leonardo DaVintik


2 Answers

I don't think you can use inline dart script inside href attribute of <a>.

However, you can add a onClick handler on your anchor :

import 'dart:html';

main() {
  AnchorElement a = query("#a1");
  a.on.click.add((e){
    FormElement form = query("#form1");
    form.submit();

    // stop event
    e.preventDefault();
    e.stopPropagation();
  });
}

And your anchor can look like :

<a id="a1">Submit</a>

If you need to retain the hand cursor (because it's now gone after taking the useless href) or style it otherwise, just use CSS:

a {
    cursor: pointer;
    color: #00f;
    text-decoration: underline;
}

a:hover {
    text-decoration: none;
}
like image 156
Alexandre Ardhuin Avatar answered Oct 17 '22 21:10

Alexandre Ardhuin


Alexandre Ardhuin gived you a good head start, but you have to use a <input type="submit" value="Submit"> to allow the user the use the enter key on their mobile device or on their keyboard:

<form>
  <input type="submit" value="Submit">
</form>

And dart code:

main() {
  Element form = query("form");
  form.on.submit.add((e){
    // Do your stuff here...

    // Stop event
    e.preventDefault();
    e.stopPropagation();
    return false;
  });
}
like image 35
darkzangel Avatar answered Oct 17 '22 23:10

darkzangel