0

I am working with dart and have something like this in my html file:

<button doStuff="alert('test')"></button>

And I want to do something like dart:

myButton.doStuff();

How would I do this. The code must be with HTML Tag, preferably . I do not want to create any custom or third party UI elements like polymer.

Thanks :)

edit:

The problem has changed, sorry, this is how I want to do:

//javascript code in the head of html
var htmlTag = getElementById('htmlTag');
htmlTag.doStuff(function() { 
   alert('test');
});

<!-- HTML code/ doStuff method not mentioned in the tag anymore  -->
<h1 id="htmlTag">

//dart code to call method doStuff
querySelector('#htmlTag')..doStuff();

How can I call doStuff() method from dart with the given element button1. Thanks

edit 2: I changed the html tag, because the button confused some people

7
  • 1
    Why do you want to do this? Commented May 22, 2014 at 8:54
  • This doesn't make more sense. Can you please explain what you actually try to accomplish? I assumed you want to execute code when someone clicks the button. Is that not what you want? In this case you have to listen to the click event and pass a callback that gets executed when the click event occurs. To make your getElementById('button1') to work you should add the id to the button <button id='button1'>text</button> Commented May 22, 2014 at 9:16
  • No I do not want to check the click event. I want any html tag, it could also be a <h1> to have an callback method like doStuff, and this is implemented in the header of the html file shown in my edited question. this doStuff should be called from dart, no clicking, no draggin no mouse... only call method when I want to :) So there must be a nice dart code like this htmlTagName.doStuff(). I hope it's clearer now. thanks Commented May 22, 2014 at 9:21
  • This still doesn't make sense to me and no, this doesn't work in Dart because Dart has no eval and therefore can't execute arbitrary strings as code. It would help a lot if you could explain what purpose this construct should fulfill. Commented May 22, 2014 at 9:49
  • 1
    I agree with Günter here. This does not make sense. Running a function within an interval can be done with the async's Timer class from dart. Instead of calling doStuff you could run a method that gets the button as a parameter. Same story, much prettier. Commented May 22, 2014 at 12:15

2 Answers 2

1

This doesn't work in plain Dart/HTML. Polymer and Angular provide support for such bindings (probably also many other frameworks).

You can add the binding in code like:

querySelector('button').onClick.listen((e) => doStuff());
Sign up to request clarification or add additional context in comments.

4 Comments

But this does not execute the specific code for that element, right? doStuff() has to be a function somewhere specified in dart.
What element? This is plain Dart ;-) The question doesn't contain anything about Polymer so I tried to show a solution that works without.
He has a button element. And the button element has an attribute called "doStuff" that contains code. And we wants to execute this code. Your code does execute a function and not the code in the attribute. This does not make sense in general but your answer does not answer his question.
I see what you mean but your interpretation doesn't make more sense to like my own ;-) But I see why you added your comment to the question.
1

It sounds like you really want to create a custom element with a doStuff method. Try this:

@CustomTag('my-button')
class MyButton extends ButtonElement with Polymer {
  MyButton.created() : super.created();

  doStuff() => alert('test');
}
<my-button id="htmlTag"></my-button>
@initMethod
main() {
  querySelector('#htmlTag').doStuff();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.