Emberjs - Get attribute of element clicked
-
28-10-2019 - |
Pergunta
Question: Upon click, I see that evt.target.attributes stores all of the attributes in an array. Is there an easier way without having to iterate all of the attributes to get a certain attributes value? In this example I need the value of the 'note' attribute for the element clicked.
Template:
<a note="C" {{action "play" on="click"}}>></a>
Click handler(play):
var keys = Ember.View.create({
templateName: 'keys',
notes: this.get('allKeys'),
play:function(evt){
var attributes= evt.target.attributes;
console.log(attributes);
}
Solução
If a controller isn't backing the view, one solution is to turn the event target into a jQuery object
play : function(event) {
var note = $(event.target).attr("note");
// More code here
}
Outras dicas
Another way is to pass the attribute value as parameter to the action.
<a note="C" onClick={{action "play" value="target.note"}}></a>
And you can access it like this:
...
actions: {
play(note) {
console.log(note);
},
},
...
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow