Chico Chico - 7 months ago 15
Javascript Question

How to use a dynamic id variable in a template.find(input:hidden[id='SOME VARIABLE']) declaration?

I wanna know if it's possible do something like this:

In HTML I have:

<input type="hidden" value="{{productName}}" id="{{productId}}">
<button type="button" class="buyProduct" value="{{productId}}">Buy</button>


In Javascript (Meteor) I wanna do:

Template.productsList.events({
click .buyProduct: function(event, template){
var idGeneratedByBtnClick = event.target.value;
console.log(idGeneratedByBtnClick); // it shows the correct ID for each button
var element = template.find('input:hidden[id='idGeneratedByBtnClick']');
});


Can i use a variable like this:
input:hidden[id='My Variable here']
???

Thanks

Answer

I think it's available to use the variable as string. Could you try?

var idGeneratedByBtnClick = event.target.value;
var element = template.find('input:hidden[id=' + idGeneratedByBtnClick + ']');