flimflam57
flimflam57

Reputation: 1334

Access dynamic id in template in Meteor

I have a Meteor template that has a selector id="{{_id}}". It gets the current teacher name object and assigns the id value of that teacher to the id of the selector. There's more than one selector per teacher, but this is one example:

<template name="oneTeacher">
  {{#each TeacherNames}}
  <div class="row">
    <div class="input-field col s2">
      <select id="{{_id}}">
        <option value="" disabled selected>Week</option>
        <option value="Week1">Week 1</option>
        ...

How can I get that id value inside a change function with a Meteor event? It would need to be something like:

Template.oneTeacher.events({
  'change #this._id': function () { // doesn't work
    console.log('changed');
  }
});

Upvotes: 0

Views: 265

Answers (1)

MasterAM
MasterAM

Reputation: 16478

Give your field something that can be used as a selector, such as a class attribute.

<template name="oneTeacher">
  {{#each TeacherNames}}
  <div class="row">
    <div class="input-field col s2">
      <select class="week-select">
        <option value="" disabled selected>Week</option>
        <option value="Week1">Week 1</option>

since it is within an #each clause, it will be bound to the relevant array element's value.

Template.oneTeacher.events({
  'change .week-select'(e, tpl) {
    console.log(this);
  }
});

The value of this will be the respective TeacherNames entry.

Upvotes: 1

Related Questions