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?

2 Answers 2

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 Commented Nov 22, 2016 at 7:55
  • How can i get component value in js file? Commented Nov 22, 2016 at 10:52
  • 1
    use $(this).val(); code Commented Nov 22, 2016 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? Commented Nov 22, 2016 at 12:07
  • @SnehaPanchal updated answer please check it Commented Nov 22, 2016 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 Commented Nov 22, 2016 at 9:42
  • then use the event attribute in submit button. Commented Nov 22, 2016 at 10:13
  • <button class="action primary checkout" type="submit" data-bind=" click: myfunction, attr: {title: $t('title')}, )} " data-role="review-save"> </button> Commented Nov 22, 2016 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.