Binding Events On Dynamically Generated DOM Elements Knockout + Magento 2












0















I am working on a module where i have generated few DOM elements dynamically. But when i am trying to call certain function on some events then its not getting called. Here is the code sample. Here you can see i am calling getData method on "change" event but its not being called



getData: function() {
var data = this._super();
data.additional_data.bank_type = $('input[name="payment\[bank_type\]"]:checked').val();
data.additional_data.emi = $('input[name="payment\[emi\]"]:checked').val();
data.additional_data.email = this.getEmail();
console.log($('input[name="payment\[emi\]"]:checked').val());
return data;
},

getEmi: function (emiCount) {
var total = priceUtils.formatPrice(quote.totals().grand_total, quote.getPriceFormat());
var rowTotal = quote.totals().grand_total;
var item = '';
var html = '';
var i;
for (i=1; i<=emiCount; i++ ) {
if (i==1) {
item = "<li>" +
"<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" +
"<label class='label' for='emi" + i + "'>Tek Ödeme, Toplam: "+total+"</label>" +
"</li>";
} else {
rowTotal = parseInt(rowTotal)/i;
item = "<li>" +
"<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" +
"<label class='label' for='emi" + i + "'>"+i+" Taksit, Aylık Taksit: "+priceUtils.formatPrice(rowTotal, quote.getPriceFormat())+" Toplam: "+total+"</label>" +
"</li>";
}
html = html+item;
}

console.log(html);
$('.emi-list').html(html);
},









share|improve this question














bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    0















    I am working on a module where i have generated few DOM elements dynamically. But when i am trying to call certain function on some events then its not getting called. Here is the code sample. Here you can see i am calling getData method on "change" event but its not being called



    getData: function() {
    var data = this._super();
    data.additional_data.bank_type = $('input[name="payment\[bank_type\]"]:checked').val();
    data.additional_data.emi = $('input[name="payment\[emi\]"]:checked').val();
    data.additional_data.email = this.getEmail();
    console.log($('input[name="payment\[emi\]"]:checked').val());
    return data;
    },

    getEmi: function (emiCount) {
    var total = priceUtils.formatPrice(quote.totals().grand_total, quote.getPriceFormat());
    var rowTotal = quote.totals().grand_total;
    var item = '';
    var html = '';
    var i;
    for (i=1; i<=emiCount; i++ ) {
    if (i==1) {
    item = "<li>" +
    "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" +
    "<label class='label' for='emi" + i + "'>Tek Ödeme, Toplam: "+total+"</label>" +
    "</li>";
    } else {
    rowTotal = parseInt(rowTotal)/i;
    item = "<li>" +
    "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" +
    "<label class='label' for='emi" + i + "'>"+i+" Taksit, Aylık Taksit: "+priceUtils.formatPrice(rowTotal, quote.getPriceFormat())+" Toplam: "+total+"</label>" +
    "</li>";
    }
    html = html+item;
    }

    console.log(html);
    $('.emi-list').html(html);
    },









    share|improve this question














    bumped to the homepage by Community 3 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      0












      0








      0








      I am working on a module where i have generated few DOM elements dynamically. But when i am trying to call certain function on some events then its not getting called. Here is the code sample. Here you can see i am calling getData method on "change" event but its not being called



      getData: function() {
      var data = this._super();
      data.additional_data.bank_type = $('input[name="payment\[bank_type\]"]:checked').val();
      data.additional_data.emi = $('input[name="payment\[emi\]"]:checked').val();
      data.additional_data.email = this.getEmail();
      console.log($('input[name="payment\[emi\]"]:checked').val());
      return data;
      },

      getEmi: function (emiCount) {
      var total = priceUtils.formatPrice(quote.totals().grand_total, quote.getPriceFormat());
      var rowTotal = quote.totals().grand_total;
      var item = '';
      var html = '';
      var i;
      for (i=1; i<=emiCount; i++ ) {
      if (i==1) {
      item = "<li>" +
      "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" +
      "<label class='label' for='emi" + i + "'>Tek Ödeme, Toplam: "+total+"</label>" +
      "</li>";
      } else {
      rowTotal = parseInt(rowTotal)/i;
      item = "<li>" +
      "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" +
      "<label class='label' for='emi" + i + "'>"+i+" Taksit, Aylık Taksit: "+priceUtils.formatPrice(rowTotal, quote.getPriceFormat())+" Toplam: "+total+"</label>" +
      "</li>";
      }
      html = html+item;
      }

      console.log(html);
      $('.emi-list').html(html);
      },









      share|improve this question














      I am working on a module where i have generated few DOM elements dynamically. But when i am trying to call certain function on some events then its not getting called. Here is the code sample. Here you can see i am calling getData method on "change" event but its not being called



      getData: function() {
      var data = this._super();
      data.additional_data.bank_type = $('input[name="payment\[bank_type\]"]:checked').val();
      data.additional_data.emi = $('input[name="payment\[emi\]"]:checked').val();
      data.additional_data.email = this.getEmail();
      console.log($('input[name="payment\[emi\]"]:checked').val());
      return data;
      },

      getEmi: function (emiCount) {
      var total = priceUtils.formatPrice(quote.totals().grand_total, quote.getPriceFormat());
      var rowTotal = quote.totals().grand_total;
      var item = '';
      var html = '';
      var i;
      for (i=1; i<=emiCount; i++ ) {
      if (i==1) {
      item = "<li>" +
      "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" +
      "<label class='label' for='emi" + i + "'>Tek Ödeme, Toplam: "+total+"</label>" +
      "</li>";
      } else {
      rowTotal = parseInt(rowTotal)/i;
      item = "<li>" +
      "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" +
      "<label class='label' for='emi" + i + "'>"+i+" Taksit, Aylık Taksit: "+priceUtils.formatPrice(rowTotal, quote.getPriceFormat())+" Toplam: "+total+"</label>" +
      "</li>";
      }
      html = html+item;
      }

      console.log(html);
      $('.emi-list').html(html);
      },






      magento2 knockoutjs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 22 '18 at 20:20









      Abhijeet kumar sharmaAbhijeet kumar sharma

      13313




      13313





      bumped to the homepage by Community 3 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 3 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You did not mention on which html element you are trying to call change event.Can you tell me more about binding template.






          share|improve this answer
























          • please have a look at getEmi method. I have bing the method getData in html item = "<li>" + "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" + "<label class='label' for='emi" + i + "'>Tek Ödeme, Toplam: "+total+"</label>" + "</li>";

            – Abhijeet kumar sharma
            Jul 26 '18 at 13:58











          • I used the same radio element in my code and it is working fine and calling the function of JS. I used this in .html file in which we binds the data.Put like this <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value=""> <label class='label' for='emi'>Test</label> </li> I am also editing my post what i have the code.

            – Charul Tyagi
            Jul 27 '18 at 4:36





















          0














          I used js code like.



          /**
          * Copyright © 2015 Magento. All rights reserved.
          * See COPYING.txt for license details.
          */
          /*jshint browser:true jquery:true*/
          /*global alert*/
          define([
          "jquery",
          'ko',
          'uiComponent',
          'Magento_Ui/js/modal/alert',
          "jquery/ui",
          "mage/translate",
          "mage/mage",
          "mage/validation"
          ], function ($, ko, Component, alert, mage) {
          "use strict";
          var totalCustomer= ko.observableArray();
          var results= ko.observableArray();
          function feedShow(name, email) {
          var self = this;
          self.name = name;
          self.email = email;
          }
          return Component.extend({
          productList: ko.observableArray(),
          initialize: function() {
          this._super();
          },
          /** Initialize observable properties */
          initObservable: function () {
          this._super()
          .observe('msgSaved')
          ;
          this.uname = ko.observable('');
          this.email = ko.observable('');
          this.phone = ko.observable('');
          this.msg = ko.observable('');
          return this;
          },
          /**
          * Validate feedback form
          */
          validateForm: function () {
          var form = '#feedback-form';
          return $(form).validation() && $(form).validation('isValid');
          },
          submitFeedback: function () {
          /* $.ajax(this.getFeedbackUrl).done(
          function() {
          this.msgSaved(true)
          }.bind(this)
          );*/
          if (!this.validateForm()) {
          return;
          }
          var data = {'name':this.uname(),'email':this.email(),'phone':this.phone(),'message':this.msg(),'status':0};
          // if(this.validateForm()){

          // }
          // console.log(data);
          $.ajax({
          url: this.getFeedbackUrl,
          data: data,
          type: 'post',
          dataType: 'json',
          context: this,
          beforeSend: this._ajaxBeforeSend,
          success: function (response) {
          this.msgSaved(true);
          this.uname('testing');
          alert({
          content: $.mage.__('Thanks for Submitting.')
          });
          },
          complete: this._ajaxComplete
          });
          },
          totalfeeds:function(){
          console.log('test');
          $.ajax({
          url:'http://magento2.local/feedback/entry/show',
          type: 'post',
          dataType: 'json',
          contentType: 'application/json',
          context: this,
          showLoader:true,
          beforeSend: this._ajaxBeforeSend,
          success: function (response) {
          var jsonTempStringified = JSON.stringify(response);
          var jsonTempStringifiedParsed = JSON.parse(jsonTempStringified);
          this.productList(jsonTempStringifiedParsed);
          // results(jsonTempStringifiedParsed);
          },
          complete: this._ajaxComplete
          });

          },
          deleteFeeds:function(parent,data){
          alert({
          content: 'Are you sure want to delete record!'
          });
          var jsonTempStringified = JSON.stringify(data);
          var jsonTempStringifiedParsed = JSON.parse(jsonTempStringified);
          var fid = jsonTempStringifiedParsed.fid;
          var data = {'fid':fid};
          $.ajax({
          url:'http://magento2.local/feedback/entry/delete',
          data: data,
          type: 'post',
          dataType: 'json',
          context: this,
          beforeSend: this._ajaxBeforeSend,
          success: function (response) {
          this.totalfeeds();
          },
          complete: this._ajaxComplete
          });
          },
          defaults: {
          msgSaved: false,
          template: 'Smart_Feedback/feedback',
          }
          });
          }
          );


          Binding template .html file.



          <!--
          /**
          * Copyright © 2015 Magento. All rights reserved.
          * See COPYING.txt for license details.
          */
          -->
          <div>
          <!-- ko ifnot: msgSaved -->
          <form method="post" class="feedback" id="feedback-form" data-role="feedback-form" data-bind="submit: submitFeedback">
          <fieldset class="fieldset">
          <legend class="legend"><span><p data-bind="i18n: 'You can post your feedback here.'"></p></span></legend><br>
          <div class="field name required">
          <label for="name" class="label"><span>Name</span></label>
          <div class="control">
          <input type="text" data-validate="{required:true}"
          class="input-text" title="Name"
          id="user_name" aria-required="true"
          data-bind="value:uname,attr: {'data-validate': JSON.stringify({required:true})},
          ">
          </div>
          </div>
          <div class="field email required">
          <label for="email" class="label"><span>Email</span></label>
          <div class="control">
          <input type="email" data-validate="{required:true}" data-bind="value:email,attr: {'data-validate': JSON.stringify({required:true,'validate-email':true})},
          " class="input-text" title="Email" id="email" aria-required="true" >
          </div>
          </div>
          <div class="field telephone">
          <label for="telephone" class="label"><span>Phone Number</span></label>
          <div class="control">
          <input type="text" class="input-text" value="" title="Phone Number" id="telephone" data-bind="value:phone">
          </div>
          </div>
          <div class="field comment required">
          <label for="comment" class="label"><span>Feedback message</span></label>
          <div class="control">
          <textarea data-validate="{required:true}" rows="3" cols="5" class="input-text" title="What’s on your mind?" id="comment" name="comment" aria-required="true"
          data-bind="value:msg"></textarea>
          </div>
          </div>
          </fieldset>
          <div class="actions-toolbar">
          <div class="primary">
          <input type="hidden" value="" id="hideit" name="hideit">
          <input type="submit" class="action primary" data-bind="value: $t('Submit Feedback')" />
          <button data-bind="attr: {title: $t('totalfeeds')}, click: totalfeeds" class="action primary">Total Feeds</button>
          <button data-bind="click: totalfeeds">click me</button>
          <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value="">
          <label class='label' for='emi'>Test</label>
          </li>
          </div>
          </div>
          </form>
          <table>
          <thead>
          <tr>
          <th>Id</th>
          <th>Name</th>
          <th>Email</th>
          <th>Action</th>
          </tr>
          </thead>
          <tbody data-bind="foreach: productList">
          <tr>
          <td data-bind="text: fid"></td>
          <td data-bind="text: name"></td>
          <td data-bind="text: email"></td>
          <td><a href="#" data-bind="click: function(data,event){$parent.deleteFeeds($parent,data);}">Remove</a></td>
          </tr>
          </tbody>
          </table>


          <!-- /ko -->

          <!-- ko if: msgSaved -->
          <p data-bind="i18n: 'Feedback message posted for review.'"></p>
          <!-- /ko -->
          </div>


          Please check above radio element with change event with Test Label.






          share|improve this answer
























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f235443%2fbinding-events-on-dynamically-generated-dom-elements-knockout-magento-2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You did not mention on which html element you are trying to call change event.Can you tell me more about binding template.






            share|improve this answer
























            • please have a look at getEmi method. I have bing the method getData in html item = "<li>" + "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" + "<label class='label' for='emi" + i + "'>Tek Ödeme, Toplam: "+total+"</label>" + "</li>";

              – Abhijeet kumar sharma
              Jul 26 '18 at 13:58











            • I used the same radio element in my code and it is working fine and calling the function of JS. I used this in .html file in which we binds the data.Put like this <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value=""> <label class='label' for='emi'>Test</label> </li> I am also editing my post what i have the code.

              – Charul Tyagi
              Jul 27 '18 at 4:36


















            0














            You did not mention on which html element you are trying to call change event.Can you tell me more about binding template.






            share|improve this answer
























            • please have a look at getEmi method. I have bing the method getData in html item = "<li>" + "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" + "<label class='label' for='emi" + i + "'>Tek Ödeme, Toplam: "+total+"</label>" + "</li>";

              – Abhijeet kumar sharma
              Jul 26 '18 at 13:58











            • I used the same radio element in my code and it is working fine and calling the function of JS. I used this in .html file in which we binds the data.Put like this <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value=""> <label class='label' for='emi'>Test</label> </li> I am also editing my post what i have the code.

              – Charul Tyagi
              Jul 27 '18 at 4:36
















            0












            0








            0







            You did not mention on which html element you are trying to call change event.Can you tell me more about binding template.






            share|improve this answer













            You did not mention on which html element you are trying to call change event.Can you tell me more about binding template.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 26 '18 at 4:02









            Charul TyagiCharul Tyagi

            705113




            705113













            • please have a look at getEmi method. I have bing the method getData in html item = "<li>" + "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" + "<label class='label' for='emi" + i + "'>Tek Ödeme, Toplam: "+total+"</label>" + "</li>";

              – Abhijeet kumar sharma
              Jul 26 '18 at 13:58











            • I used the same radio element in my code and it is working fine and calling the function of JS. I used this in .html file in which we binds the data.Put like this <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value=""> <label class='label' for='emi'>Test</label> </li> I am also editing my post what i have the code.

              – Charul Tyagi
              Jul 27 '18 at 4:36





















            • please have a look at getEmi method. I have bing the method getData in html item = "<li>" + "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" + "<label class='label' for='emi" + i + "'>Tek Ödeme, Toplam: "+total+"</label>" + "</li>";

              – Abhijeet kumar sharma
              Jul 26 '18 at 13:58











            • I used the same radio element in my code and it is working fine and calling the function of JS. I used this in .html file in which we binds the data.Put like this <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value=""> <label class='label' for='emi'>Test</label> </li> I am also editing my post what i have the code.

              – Charul Tyagi
              Jul 27 '18 at 4:36



















            please have a look at getEmi method. I have bing the method getData in html item = "<li>" + "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" + "<label class='label' for='emi" + i + "'>Tek Ödeme, Toplam: "+total+"</label>" + "</li>";

            – Abhijeet kumar sharma
            Jul 26 '18 at 13:58





            please have a look at getEmi method. I have bing the method getData in html item = "<li>" + "<input type='radio' name='payment[emi_type]' data-bind='event: { change: getData}' id='emi" + i + "' value='" + i + "'>" + "<label class='label' for='emi" + i + "'>Tek Ödeme, Toplam: "+total+"</label>" + "</li>";

            – Abhijeet kumar sharma
            Jul 26 '18 at 13:58













            I used the same radio element in my code and it is working fine and calling the function of JS. I used this in .html file in which we binds the data.Put like this <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value=""> <label class='label' for='emi'>Test</label> </li> I am also editing my post what i have the code.

            – Charul Tyagi
            Jul 27 '18 at 4:36







            I used the same radio element in my code and it is working fine and calling the function of JS. I used this in .html file in which we binds the data.Put like this <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value=""> <label class='label' for='emi'>Test</label> </li> I am also editing my post what i have the code.

            – Charul Tyagi
            Jul 27 '18 at 4:36















            0














            I used js code like.



            /**
            * Copyright © 2015 Magento. All rights reserved.
            * See COPYING.txt for license details.
            */
            /*jshint browser:true jquery:true*/
            /*global alert*/
            define([
            "jquery",
            'ko',
            'uiComponent',
            'Magento_Ui/js/modal/alert',
            "jquery/ui",
            "mage/translate",
            "mage/mage",
            "mage/validation"
            ], function ($, ko, Component, alert, mage) {
            "use strict";
            var totalCustomer= ko.observableArray();
            var results= ko.observableArray();
            function feedShow(name, email) {
            var self = this;
            self.name = name;
            self.email = email;
            }
            return Component.extend({
            productList: ko.observableArray(),
            initialize: function() {
            this._super();
            },
            /** Initialize observable properties */
            initObservable: function () {
            this._super()
            .observe('msgSaved')
            ;
            this.uname = ko.observable('');
            this.email = ko.observable('');
            this.phone = ko.observable('');
            this.msg = ko.observable('');
            return this;
            },
            /**
            * Validate feedback form
            */
            validateForm: function () {
            var form = '#feedback-form';
            return $(form).validation() && $(form).validation('isValid');
            },
            submitFeedback: function () {
            /* $.ajax(this.getFeedbackUrl).done(
            function() {
            this.msgSaved(true)
            }.bind(this)
            );*/
            if (!this.validateForm()) {
            return;
            }
            var data = {'name':this.uname(),'email':this.email(),'phone':this.phone(),'message':this.msg(),'status':0};
            // if(this.validateForm()){

            // }
            // console.log(data);
            $.ajax({
            url: this.getFeedbackUrl,
            data: data,
            type: 'post',
            dataType: 'json',
            context: this,
            beforeSend: this._ajaxBeforeSend,
            success: function (response) {
            this.msgSaved(true);
            this.uname('testing');
            alert({
            content: $.mage.__('Thanks for Submitting.')
            });
            },
            complete: this._ajaxComplete
            });
            },
            totalfeeds:function(){
            console.log('test');
            $.ajax({
            url:'http://magento2.local/feedback/entry/show',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            context: this,
            showLoader:true,
            beforeSend: this._ajaxBeforeSend,
            success: function (response) {
            var jsonTempStringified = JSON.stringify(response);
            var jsonTempStringifiedParsed = JSON.parse(jsonTempStringified);
            this.productList(jsonTempStringifiedParsed);
            // results(jsonTempStringifiedParsed);
            },
            complete: this._ajaxComplete
            });

            },
            deleteFeeds:function(parent,data){
            alert({
            content: 'Are you sure want to delete record!'
            });
            var jsonTempStringified = JSON.stringify(data);
            var jsonTempStringifiedParsed = JSON.parse(jsonTempStringified);
            var fid = jsonTempStringifiedParsed.fid;
            var data = {'fid':fid};
            $.ajax({
            url:'http://magento2.local/feedback/entry/delete',
            data: data,
            type: 'post',
            dataType: 'json',
            context: this,
            beforeSend: this._ajaxBeforeSend,
            success: function (response) {
            this.totalfeeds();
            },
            complete: this._ajaxComplete
            });
            },
            defaults: {
            msgSaved: false,
            template: 'Smart_Feedback/feedback',
            }
            });
            }
            );


            Binding template .html file.



            <!--
            /**
            * Copyright © 2015 Magento. All rights reserved.
            * See COPYING.txt for license details.
            */
            -->
            <div>
            <!-- ko ifnot: msgSaved -->
            <form method="post" class="feedback" id="feedback-form" data-role="feedback-form" data-bind="submit: submitFeedback">
            <fieldset class="fieldset">
            <legend class="legend"><span><p data-bind="i18n: 'You can post your feedback here.'"></p></span></legend><br>
            <div class="field name required">
            <label for="name" class="label"><span>Name</span></label>
            <div class="control">
            <input type="text" data-validate="{required:true}"
            class="input-text" title="Name"
            id="user_name" aria-required="true"
            data-bind="value:uname,attr: {'data-validate': JSON.stringify({required:true})},
            ">
            </div>
            </div>
            <div class="field email required">
            <label for="email" class="label"><span>Email</span></label>
            <div class="control">
            <input type="email" data-validate="{required:true}" data-bind="value:email,attr: {'data-validate': JSON.stringify({required:true,'validate-email':true})},
            " class="input-text" title="Email" id="email" aria-required="true" >
            </div>
            </div>
            <div class="field telephone">
            <label for="telephone" class="label"><span>Phone Number</span></label>
            <div class="control">
            <input type="text" class="input-text" value="" title="Phone Number" id="telephone" data-bind="value:phone">
            </div>
            </div>
            <div class="field comment required">
            <label for="comment" class="label"><span>Feedback message</span></label>
            <div class="control">
            <textarea data-validate="{required:true}" rows="3" cols="5" class="input-text" title="What’s on your mind?" id="comment" name="comment" aria-required="true"
            data-bind="value:msg"></textarea>
            </div>
            </div>
            </fieldset>
            <div class="actions-toolbar">
            <div class="primary">
            <input type="hidden" value="" id="hideit" name="hideit">
            <input type="submit" class="action primary" data-bind="value: $t('Submit Feedback')" />
            <button data-bind="attr: {title: $t('totalfeeds')}, click: totalfeeds" class="action primary">Total Feeds</button>
            <button data-bind="click: totalfeeds">click me</button>
            <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value="">
            <label class='label' for='emi'>Test</label>
            </li>
            </div>
            </div>
            </form>
            <table>
            <thead>
            <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Email</th>
            <th>Action</th>
            </tr>
            </thead>
            <tbody data-bind="foreach: productList">
            <tr>
            <td data-bind="text: fid"></td>
            <td data-bind="text: name"></td>
            <td data-bind="text: email"></td>
            <td><a href="#" data-bind="click: function(data,event){$parent.deleteFeeds($parent,data);}">Remove</a></td>
            </tr>
            </tbody>
            </table>


            <!-- /ko -->

            <!-- ko if: msgSaved -->
            <p data-bind="i18n: 'Feedback message posted for review.'"></p>
            <!-- /ko -->
            </div>


            Please check above radio element with change event with Test Label.






            share|improve this answer




























              0














              I used js code like.



              /**
              * Copyright © 2015 Magento. All rights reserved.
              * See COPYING.txt for license details.
              */
              /*jshint browser:true jquery:true*/
              /*global alert*/
              define([
              "jquery",
              'ko',
              'uiComponent',
              'Magento_Ui/js/modal/alert',
              "jquery/ui",
              "mage/translate",
              "mage/mage",
              "mage/validation"
              ], function ($, ko, Component, alert, mage) {
              "use strict";
              var totalCustomer= ko.observableArray();
              var results= ko.observableArray();
              function feedShow(name, email) {
              var self = this;
              self.name = name;
              self.email = email;
              }
              return Component.extend({
              productList: ko.observableArray(),
              initialize: function() {
              this._super();
              },
              /** Initialize observable properties */
              initObservable: function () {
              this._super()
              .observe('msgSaved')
              ;
              this.uname = ko.observable('');
              this.email = ko.observable('');
              this.phone = ko.observable('');
              this.msg = ko.observable('');
              return this;
              },
              /**
              * Validate feedback form
              */
              validateForm: function () {
              var form = '#feedback-form';
              return $(form).validation() && $(form).validation('isValid');
              },
              submitFeedback: function () {
              /* $.ajax(this.getFeedbackUrl).done(
              function() {
              this.msgSaved(true)
              }.bind(this)
              );*/
              if (!this.validateForm()) {
              return;
              }
              var data = {'name':this.uname(),'email':this.email(),'phone':this.phone(),'message':this.msg(),'status':0};
              // if(this.validateForm()){

              // }
              // console.log(data);
              $.ajax({
              url: this.getFeedbackUrl,
              data: data,
              type: 'post',
              dataType: 'json',
              context: this,
              beforeSend: this._ajaxBeforeSend,
              success: function (response) {
              this.msgSaved(true);
              this.uname('testing');
              alert({
              content: $.mage.__('Thanks for Submitting.')
              });
              },
              complete: this._ajaxComplete
              });
              },
              totalfeeds:function(){
              console.log('test');
              $.ajax({
              url:'http://magento2.local/feedback/entry/show',
              type: 'post',
              dataType: 'json',
              contentType: 'application/json',
              context: this,
              showLoader:true,
              beforeSend: this._ajaxBeforeSend,
              success: function (response) {
              var jsonTempStringified = JSON.stringify(response);
              var jsonTempStringifiedParsed = JSON.parse(jsonTempStringified);
              this.productList(jsonTempStringifiedParsed);
              // results(jsonTempStringifiedParsed);
              },
              complete: this._ajaxComplete
              });

              },
              deleteFeeds:function(parent,data){
              alert({
              content: 'Are you sure want to delete record!'
              });
              var jsonTempStringified = JSON.stringify(data);
              var jsonTempStringifiedParsed = JSON.parse(jsonTempStringified);
              var fid = jsonTempStringifiedParsed.fid;
              var data = {'fid':fid};
              $.ajax({
              url:'http://magento2.local/feedback/entry/delete',
              data: data,
              type: 'post',
              dataType: 'json',
              context: this,
              beforeSend: this._ajaxBeforeSend,
              success: function (response) {
              this.totalfeeds();
              },
              complete: this._ajaxComplete
              });
              },
              defaults: {
              msgSaved: false,
              template: 'Smart_Feedback/feedback',
              }
              });
              }
              );


              Binding template .html file.



              <!--
              /**
              * Copyright © 2015 Magento. All rights reserved.
              * See COPYING.txt for license details.
              */
              -->
              <div>
              <!-- ko ifnot: msgSaved -->
              <form method="post" class="feedback" id="feedback-form" data-role="feedback-form" data-bind="submit: submitFeedback">
              <fieldset class="fieldset">
              <legend class="legend"><span><p data-bind="i18n: 'You can post your feedback here.'"></p></span></legend><br>
              <div class="field name required">
              <label for="name" class="label"><span>Name</span></label>
              <div class="control">
              <input type="text" data-validate="{required:true}"
              class="input-text" title="Name"
              id="user_name" aria-required="true"
              data-bind="value:uname,attr: {'data-validate': JSON.stringify({required:true})},
              ">
              </div>
              </div>
              <div class="field email required">
              <label for="email" class="label"><span>Email</span></label>
              <div class="control">
              <input type="email" data-validate="{required:true}" data-bind="value:email,attr: {'data-validate': JSON.stringify({required:true,'validate-email':true})},
              " class="input-text" title="Email" id="email" aria-required="true" >
              </div>
              </div>
              <div class="field telephone">
              <label for="telephone" class="label"><span>Phone Number</span></label>
              <div class="control">
              <input type="text" class="input-text" value="" title="Phone Number" id="telephone" data-bind="value:phone">
              </div>
              </div>
              <div class="field comment required">
              <label for="comment" class="label"><span>Feedback message</span></label>
              <div class="control">
              <textarea data-validate="{required:true}" rows="3" cols="5" class="input-text" title="What’s on your mind?" id="comment" name="comment" aria-required="true"
              data-bind="value:msg"></textarea>
              </div>
              </div>
              </fieldset>
              <div class="actions-toolbar">
              <div class="primary">
              <input type="hidden" value="" id="hideit" name="hideit">
              <input type="submit" class="action primary" data-bind="value: $t('Submit Feedback')" />
              <button data-bind="attr: {title: $t('totalfeeds')}, click: totalfeeds" class="action primary">Total Feeds</button>
              <button data-bind="click: totalfeeds">click me</button>
              <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value="">
              <label class='label' for='emi'>Test</label>
              </li>
              </div>
              </div>
              </form>
              <table>
              <thead>
              <tr>
              <th>Id</th>
              <th>Name</th>
              <th>Email</th>
              <th>Action</th>
              </tr>
              </thead>
              <tbody data-bind="foreach: productList">
              <tr>
              <td data-bind="text: fid"></td>
              <td data-bind="text: name"></td>
              <td data-bind="text: email"></td>
              <td><a href="#" data-bind="click: function(data,event){$parent.deleteFeeds($parent,data);}">Remove</a></td>
              </tr>
              </tbody>
              </table>


              <!-- /ko -->

              <!-- ko if: msgSaved -->
              <p data-bind="i18n: 'Feedback message posted for review.'"></p>
              <!-- /ko -->
              </div>


              Please check above radio element with change event with Test Label.






              share|improve this answer


























                0












                0








                0







                I used js code like.



                /**
                * Copyright © 2015 Magento. All rights reserved.
                * See COPYING.txt for license details.
                */
                /*jshint browser:true jquery:true*/
                /*global alert*/
                define([
                "jquery",
                'ko',
                'uiComponent',
                'Magento_Ui/js/modal/alert',
                "jquery/ui",
                "mage/translate",
                "mage/mage",
                "mage/validation"
                ], function ($, ko, Component, alert, mage) {
                "use strict";
                var totalCustomer= ko.observableArray();
                var results= ko.observableArray();
                function feedShow(name, email) {
                var self = this;
                self.name = name;
                self.email = email;
                }
                return Component.extend({
                productList: ko.observableArray(),
                initialize: function() {
                this._super();
                },
                /** Initialize observable properties */
                initObservable: function () {
                this._super()
                .observe('msgSaved')
                ;
                this.uname = ko.observable('');
                this.email = ko.observable('');
                this.phone = ko.observable('');
                this.msg = ko.observable('');
                return this;
                },
                /**
                * Validate feedback form
                */
                validateForm: function () {
                var form = '#feedback-form';
                return $(form).validation() && $(form).validation('isValid');
                },
                submitFeedback: function () {
                /* $.ajax(this.getFeedbackUrl).done(
                function() {
                this.msgSaved(true)
                }.bind(this)
                );*/
                if (!this.validateForm()) {
                return;
                }
                var data = {'name':this.uname(),'email':this.email(),'phone':this.phone(),'message':this.msg(),'status':0};
                // if(this.validateForm()){

                // }
                // console.log(data);
                $.ajax({
                url: this.getFeedbackUrl,
                data: data,
                type: 'post',
                dataType: 'json',
                context: this,
                beforeSend: this._ajaxBeforeSend,
                success: function (response) {
                this.msgSaved(true);
                this.uname('testing');
                alert({
                content: $.mage.__('Thanks for Submitting.')
                });
                },
                complete: this._ajaxComplete
                });
                },
                totalfeeds:function(){
                console.log('test');
                $.ajax({
                url:'http://magento2.local/feedback/entry/show',
                type: 'post',
                dataType: 'json',
                contentType: 'application/json',
                context: this,
                showLoader:true,
                beforeSend: this._ajaxBeforeSend,
                success: function (response) {
                var jsonTempStringified = JSON.stringify(response);
                var jsonTempStringifiedParsed = JSON.parse(jsonTempStringified);
                this.productList(jsonTempStringifiedParsed);
                // results(jsonTempStringifiedParsed);
                },
                complete: this._ajaxComplete
                });

                },
                deleteFeeds:function(parent,data){
                alert({
                content: 'Are you sure want to delete record!'
                });
                var jsonTempStringified = JSON.stringify(data);
                var jsonTempStringifiedParsed = JSON.parse(jsonTempStringified);
                var fid = jsonTempStringifiedParsed.fid;
                var data = {'fid':fid};
                $.ajax({
                url:'http://magento2.local/feedback/entry/delete',
                data: data,
                type: 'post',
                dataType: 'json',
                context: this,
                beforeSend: this._ajaxBeforeSend,
                success: function (response) {
                this.totalfeeds();
                },
                complete: this._ajaxComplete
                });
                },
                defaults: {
                msgSaved: false,
                template: 'Smart_Feedback/feedback',
                }
                });
                }
                );


                Binding template .html file.



                <!--
                /**
                * Copyright © 2015 Magento. All rights reserved.
                * See COPYING.txt for license details.
                */
                -->
                <div>
                <!-- ko ifnot: msgSaved -->
                <form method="post" class="feedback" id="feedback-form" data-role="feedback-form" data-bind="submit: submitFeedback">
                <fieldset class="fieldset">
                <legend class="legend"><span><p data-bind="i18n: 'You can post your feedback here.'"></p></span></legend><br>
                <div class="field name required">
                <label for="name" class="label"><span>Name</span></label>
                <div class="control">
                <input type="text" data-validate="{required:true}"
                class="input-text" title="Name"
                id="user_name" aria-required="true"
                data-bind="value:uname,attr: {'data-validate': JSON.stringify({required:true})},
                ">
                </div>
                </div>
                <div class="field email required">
                <label for="email" class="label"><span>Email</span></label>
                <div class="control">
                <input type="email" data-validate="{required:true}" data-bind="value:email,attr: {'data-validate': JSON.stringify({required:true,'validate-email':true})},
                " class="input-text" title="Email" id="email" aria-required="true" >
                </div>
                </div>
                <div class="field telephone">
                <label for="telephone" class="label"><span>Phone Number</span></label>
                <div class="control">
                <input type="text" class="input-text" value="" title="Phone Number" id="telephone" data-bind="value:phone">
                </div>
                </div>
                <div class="field comment required">
                <label for="comment" class="label"><span>Feedback message</span></label>
                <div class="control">
                <textarea data-validate="{required:true}" rows="3" cols="5" class="input-text" title="What’s on your mind?" id="comment" name="comment" aria-required="true"
                data-bind="value:msg"></textarea>
                </div>
                </div>
                </fieldset>
                <div class="actions-toolbar">
                <div class="primary">
                <input type="hidden" value="" id="hideit" name="hideit">
                <input type="submit" class="action primary" data-bind="value: $t('Submit Feedback')" />
                <button data-bind="attr: {title: $t('totalfeeds')}, click: totalfeeds" class="action primary">Total Feeds</button>
                <button data-bind="click: totalfeeds">click me</button>
                <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value="">
                <label class='label' for='emi'>Test</label>
                </li>
                </div>
                </div>
                </form>
                <table>
                <thead>
                <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Email</th>
                <th>Action</th>
                </tr>
                </thead>
                <tbody data-bind="foreach: productList">
                <tr>
                <td data-bind="text: fid"></td>
                <td data-bind="text: name"></td>
                <td data-bind="text: email"></td>
                <td><a href="#" data-bind="click: function(data,event){$parent.deleteFeeds($parent,data);}">Remove</a></td>
                </tr>
                </tbody>
                </table>


                <!-- /ko -->

                <!-- ko if: msgSaved -->
                <p data-bind="i18n: 'Feedback message posted for review.'"></p>
                <!-- /ko -->
                </div>


                Please check above radio element with change event with Test Label.






                share|improve this answer













                I used js code like.



                /**
                * Copyright © 2015 Magento. All rights reserved.
                * See COPYING.txt for license details.
                */
                /*jshint browser:true jquery:true*/
                /*global alert*/
                define([
                "jquery",
                'ko',
                'uiComponent',
                'Magento_Ui/js/modal/alert',
                "jquery/ui",
                "mage/translate",
                "mage/mage",
                "mage/validation"
                ], function ($, ko, Component, alert, mage) {
                "use strict";
                var totalCustomer= ko.observableArray();
                var results= ko.observableArray();
                function feedShow(name, email) {
                var self = this;
                self.name = name;
                self.email = email;
                }
                return Component.extend({
                productList: ko.observableArray(),
                initialize: function() {
                this._super();
                },
                /** Initialize observable properties */
                initObservable: function () {
                this._super()
                .observe('msgSaved')
                ;
                this.uname = ko.observable('');
                this.email = ko.observable('');
                this.phone = ko.observable('');
                this.msg = ko.observable('');
                return this;
                },
                /**
                * Validate feedback form
                */
                validateForm: function () {
                var form = '#feedback-form';
                return $(form).validation() && $(form).validation('isValid');
                },
                submitFeedback: function () {
                /* $.ajax(this.getFeedbackUrl).done(
                function() {
                this.msgSaved(true)
                }.bind(this)
                );*/
                if (!this.validateForm()) {
                return;
                }
                var data = {'name':this.uname(),'email':this.email(),'phone':this.phone(),'message':this.msg(),'status':0};
                // if(this.validateForm()){

                // }
                // console.log(data);
                $.ajax({
                url: this.getFeedbackUrl,
                data: data,
                type: 'post',
                dataType: 'json',
                context: this,
                beforeSend: this._ajaxBeforeSend,
                success: function (response) {
                this.msgSaved(true);
                this.uname('testing');
                alert({
                content: $.mage.__('Thanks for Submitting.')
                });
                },
                complete: this._ajaxComplete
                });
                },
                totalfeeds:function(){
                console.log('test');
                $.ajax({
                url:'http://magento2.local/feedback/entry/show',
                type: 'post',
                dataType: 'json',
                contentType: 'application/json',
                context: this,
                showLoader:true,
                beforeSend: this._ajaxBeforeSend,
                success: function (response) {
                var jsonTempStringified = JSON.stringify(response);
                var jsonTempStringifiedParsed = JSON.parse(jsonTempStringified);
                this.productList(jsonTempStringifiedParsed);
                // results(jsonTempStringifiedParsed);
                },
                complete: this._ajaxComplete
                });

                },
                deleteFeeds:function(parent,data){
                alert({
                content: 'Are you sure want to delete record!'
                });
                var jsonTempStringified = JSON.stringify(data);
                var jsonTempStringifiedParsed = JSON.parse(jsonTempStringified);
                var fid = jsonTempStringifiedParsed.fid;
                var data = {'fid':fid};
                $.ajax({
                url:'http://magento2.local/feedback/entry/delete',
                data: data,
                type: 'post',
                dataType: 'json',
                context: this,
                beforeSend: this._ajaxBeforeSend,
                success: function (response) {
                this.totalfeeds();
                },
                complete: this._ajaxComplete
                });
                },
                defaults: {
                msgSaved: false,
                template: 'Smart_Feedback/feedback',
                }
                });
                }
                );


                Binding template .html file.



                <!--
                /**
                * Copyright © 2015 Magento. All rights reserved.
                * See COPYING.txt for license details.
                */
                -->
                <div>
                <!-- ko ifnot: msgSaved -->
                <form method="post" class="feedback" id="feedback-form" data-role="feedback-form" data-bind="submit: submitFeedback">
                <fieldset class="fieldset">
                <legend class="legend"><span><p data-bind="i18n: 'You can post your feedback here.'"></p></span></legend><br>
                <div class="field name required">
                <label for="name" class="label"><span>Name</span></label>
                <div class="control">
                <input type="text" data-validate="{required:true}"
                class="input-text" title="Name"
                id="user_name" aria-required="true"
                data-bind="value:uname,attr: {'data-validate': JSON.stringify({required:true})},
                ">
                </div>
                </div>
                <div class="field email required">
                <label for="email" class="label"><span>Email</span></label>
                <div class="control">
                <input type="email" data-validate="{required:true}" data-bind="value:email,attr: {'data-validate': JSON.stringify({required:true,'validate-email':true})},
                " class="input-text" title="Email" id="email" aria-required="true" >
                </div>
                </div>
                <div class="field telephone">
                <label for="telephone" class="label"><span>Phone Number</span></label>
                <div class="control">
                <input type="text" class="input-text" value="" title="Phone Number" id="telephone" data-bind="value:phone">
                </div>
                </div>
                <div class="field comment required">
                <label for="comment" class="label"><span>Feedback message</span></label>
                <div class="control">
                <textarea data-validate="{required:true}" rows="3" cols="5" class="input-text" title="What’s on your mind?" id="comment" name="comment" aria-required="true"
                data-bind="value:msg"></textarea>
                </div>
                </div>
                </fieldset>
                <div class="actions-toolbar">
                <div class="primary">
                <input type="hidden" value="" id="hideit" name="hideit">
                <input type="submit" class="action primary" data-bind="value: $t('Submit Feedback')" />
                <button data-bind="attr: {title: $t('totalfeeds')}, click: totalfeeds" class="action primary">Total Feeds</button>
                <button data-bind="click: totalfeeds">click me</button>
                <li><input type='radio' name='emi_type' data-bind='event: { change: totalfeeds}' id='emi' value="">
                <label class='label' for='emi'>Test</label>
                </li>
                </div>
                </div>
                </form>
                <table>
                <thead>
                <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Email</th>
                <th>Action</th>
                </tr>
                </thead>
                <tbody data-bind="foreach: productList">
                <tr>
                <td data-bind="text: fid"></td>
                <td data-bind="text: name"></td>
                <td data-bind="text: email"></td>
                <td><a href="#" data-bind="click: function(data,event){$parent.deleteFeeds($parent,data);}">Remove</a></td>
                </tr>
                </tbody>
                </table>


                <!-- /ko -->

                <!-- ko if: msgSaved -->
                <p data-bind="i18n: 'Feedback message posted for review.'"></p>
                <!-- /ko -->
                </div>


                Please check above radio element with change event with Test Label.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 27 '18 at 4:43









                Charul TyagiCharul Tyagi

                705113




                705113






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f235443%2fbinding-events-on-dynamically-generated-dom-elements-knockout-magento-2%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What other Star Trek series did the main TNG cast show up in?

                    Berlina muro

                    Berlina aerponto