Genjuro
Genjuro

Reputation: 7735

how to get the id of the clicked item

i need to get the id of the currently clicked item . i've done dirty solution for now , i store the id of the current item , in the button's id ,the retrieve the id on click on that button in the template manager.

<button type="button" id ="{{id}}" class="btn btn-info btn-sm"><i class="fa fa-eye"></i>View</button>

then in the manager i do .

    Template.formItem.events = {

  "click .btn":function (e){
    Router.go('forms.show', {_id: $(e.target).attr('id')}, {query: 'q=s', hash: 'hashFrag'});
  }
}

Upvotes: 3

Views: 4748

Answers (2)

Sander van den Akker
Sander van den Akker

Reputation: 1554

To get to the element that is clicked in a Meteor template click event, use the currentTarget property:

"click .btn":function (event) {
  Router.go('forms.show', {_id: event.currentTarget.id}, {query: 'q=s', hash: 'hashFrag'});
}

This is also valid for other events like submit, mouseover etc.

Upvotes: 3

Sasikanth
Sasikanth

Reputation: 3043

if the data context has id then,this.id will work

Try consoling this console.log(this) on click event

 "click .btn":function (e){
    console.log(this);
    Router.go('forms.show', {_id: this.id}, {query: 'q=s', hash: 'hashFrag'});
  }

EDIT

Also, read this article https://dweldon.silvrback.com/common-mistakes

This is one of the common mistakes author mentioned in that post.

Upvotes: 7

Related Questions