3

I want to call on click function in template file with script type "text/x-magento-init".

Then please tell me how can i do this?

3

Add below code in js file:

define(['jquery'], function ($) {
    'use strict';

    return function (config) { 
        console.log(config);
        $(document).on("click","input[id^='your-button-id-here']", function () {
             console.log('click');
        });   
    };
});

Add below code in template file:

<script type="text/x-magento-init">
{
   "*": {
     "Your component name here": {"url":"value"}
   }
}
</script>
6
  • 1
    I already used this but this is not standard coding.I want to use script type text/x-magento-init – Sneha Panchal Nov 22 '16 at 7:55
  • How can i get component value in js file? – Sneha Panchal Nov 22 '16 at 10:52
  • 1
    use $(this).val(); code – Prashant Valanda Nov 22 '16 at 11:56
  • Actually i want to pass url from template file and want to get url in js file then how can i get? – Sneha Panchal Nov 22 '16 at 12:07
  • @SnehaPanchal updated answer please check it – Prashant Valanda Nov 22 '16 at 12:16
3

.js file add this function

      myfunction: function () {
                $('#your_id').click(function () {
                   //your code goes here
                });
            },`

Your template data-bind should be:

 <input type="text"
                           id="your_field_id"
                           name="name"
                           data-validate="{required:true}"
                           data-bind='
                            attr: {title: $t("title")},
                            value: fieldvalue,

                            event: { "input": myfunction}'
                           class="input-text"/>
3
  • This function not call.I want to add function while click on button – Sneha Panchal Nov 22 '16 at 9:42
  • then use the event attribute in submit button. – rajat kara Nov 22 '16 at 10:13
  • <button class="action primary checkout" type="submit" data-bind=" click: myfunction, attr: {title: $t('title')}, )} " data-role="review-save"> </button> – rajat kara Nov 22 '16 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.