For example my html is
<template name="atest">
<a href="{{route}}" data-test="test">Click</a>
</template>
In meteor template helpers, I want to be able to select the anchor tag.
Template.atest.route = function() {
console.log(this.data-test);
};
I am not sure if this can be done or not, but certainly, it cannot be done via any method I have tried. I know there is a way to pass argument in a template instance, but I don't want that. I want to be able to select that anchor tag that template instance is in and do something with it.
Appreciate any help I can get.
Not in helpers, but in the
rendered
callback you can do:And in event handlers:
Of course, you could also do:
If none of these options work for you, you might want to reevaluate your approach. Needing access to an element from a helper function indicates that you are trying to use a procedural coding style rather than a template-driven style. In general, don't store data on DOM nodes, store it in the template's context object.
Could you give some additional context on what exactly you're trying to do? There might be a better way.
Think about this: the helper has to be called in order to render the element. How would you be able to access the element if it doesn't even exist yet?
Edit: here is a template-driven approach to attaching
href
attributes to a template depending on where it is defined. Basically, you want to include the necessary data to generate the link template in any associated parent template. Then, just call the link template with that data:HTML:
JS: