Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass DOM element to Aurelia function?

Tags:

aurelia

I'm trying to pass the DOM element to Aurelia function, but it ain't work. Just to notice that this element is not in the 'repeat.for' statement.

I have something like this, which obviously is not correct :

<span click.trigger="passDom(d)">Pass Dom Element</span>


export class Test {

  passDom(d) {
    console.log(d);
  }
}

I try using $self, but not working as well.

like image 823
Kaloyan Stamatov Avatar asked Aug 15 '16 07:08

Kaloyan Stamatov


1 Answers

Use $event.target like this:

<span click.trigger="passDom($event.target)">Pass Dom Element</span>

You can read more about DOM events in Aurelia Hub - DOM Events.

Use the special $event property to access the DOM event in your binding expression.

Alternatively, you can create reference to DOM event and use it from view model:

<span click.trigger="passDom()" ref="myElement">Pass Dom Element</span>

// in view model
export class Test {
  passDom() {
    console.log(this.myElement);
  }
}

Also available in Aurelia Hub - Referencing Elements.

Use the ref binding command to create a reference to a DOM element. The ref command's most basic syntax is ref="expression". When the view is data-bound the specified expression will be assigned the DOM element.

like image 67
Miroslav Popovic Avatar answered Oct 21 '22 16:10

Miroslav Popovic