1

When I located a DOM element via:

$('md-autocomplete-wrap button')[0]

which the browser console actually reveals correctly to be an HTML button, I'd like to click this button through a controller action. Nevertheless:

$('md-autocomplete-wrap button')[0].click();

doesn't do the trick as nothing happens. How do I correctly trigger the click() function of this button?

mx0
  • 6,445
  • 12
  • 49
  • 54
Sedrunum
  • 11
  • 3
  • Is the DOM element you are trying to click something rendered by Ember? If it is, I'd suggest doing this a different way ... – acorncom Aug 10 '17 at 03:08

2 Answers2

0

Use this code:

http://emberjs.jsbin.com/mimuwale/1/edit

js

App = Ember.Application.create();

function getView($el){
  return Ember.View.views[$el.closest(".ember-view").attr("id")];
}

function newAlert(el){
  getView($(el)).get('controller').send('newAlert',el.id);
}

App.IndexView=Ember.View.extend();

App.IndexController=Ember.Controller.extend({
  actions:{
    newAlert:function(buttonId){alert('clicked button:'+buttonId);}
  }
});

hbs

<script type="text/x-handlebars" data-template-name="index">
  <button id='definition' onclick='newAlert(this)'>test</button>
</script>
Chandra Kumar
  • 4,127
  • 1
  • 17
  • 25
  • I don't want to overwrite the default onClick method of this button but actually make use of it through calling the click() function. – Sedrunum Aug 06 '17 at 16:40
0

Use jquery trigger method

$('md-autocomplete-wrap button')[0].trigger('click')

Reference:
http://api.jquery.com/trigger/

Ember Freak
  • 12,918
  • 4
  • 24
  • 54