I'm looking for a way to modify the default dehavior of a select template.
I just want to add a button and keeping what's already done by magento team. So, I copied the template select.phtml in my extension but it is a "text/x-magento-template" and not a "text/x-magento-init". And I found very few resources about this javascript templating system.
I tried that in a test.js:
require(['jquery'], function ($) {
$("input[id^='btn-test']").click( function () {
console.log('click');
});
});
And this is my select.phtml :
<h1>Template test</h1>
<input type="button" id="btn-test-1" value="OK" />
<script id="custom-option-select-type-template" type="text/x-magento-template">
<div id="product_option_<%- data.option_id %>_type_<%- data.group %>" class="fieldset">
<input type="button" id="btn-test-2" value="OK" />
</div>
</script>
<script type="text/x-magento-init">
{
"*": {
"HappyDev_Test/test": {}
}
}
</script>
Clicking the first button works as expected but the one in the javascript template doesn't produce anything.
How can I do to insert my own code in the javascript template ?
Best Answer
Your code bind the click event only to the elements that exist at the time of binding. Replace your
test.js
code with below one: