Change event does not work with dynamically generated elements - jQuery

I am generating a dynamic dropdownList dynamically using jquery Ajax generated dropdown id specificationAttribute. I want to create an add event for a new tag ( specificationAttribute), for this I created Belowe scriptin window.load:

$(document).on('change', '#specificationattribute', function () {
    alert("Clicked Me !");
});

but that will not work. I try more like click, livebut I can’t get the result.

jsfiddle

Script code:

$(window).load(function () {
  $("#specificationCategory").change(function () {
        var selected = $(this).find(":selected");
        if (selected.val().trim().length == 0) {
            ShowMessage('please selecet ...', 'information');
        }
        else {

            var categoryId = selected.val();
            var url = $('#url').data('loadspecificationattributes');

            $.ajax({
                url: url,
                data: { categoryId: categoryId, controlId: 'specificationattribute' },
                type: 'POST',
                success: function (data) {
                    $('#specificationattributes').html(data);
                },
                error: function (response) {
                    alert(response.error);

                }
            });

        }

    });



    $(document).on('change', '#specificationAttribute', function () {
        alert("changed ");

    });
    }
+4
source share
3 answers

Your violin has syntax errors. Since the drop-down list generates a selection, let it be used.

HTML, :

<select id="specificationAttribute" name="specificationAttribute">
</select>

: (. , - , )

$(window).on('load', function() {
  $("#specificationCategory").on('change',function() {
    var selected = $(this).find(":selected");
    // if there is a selection, this should have a length so use that
    // old:  if (selected.val().trim().length == 0) {
    if (!selected.length) { // new
    // NO clue what this is and not on the fiddle so commented it out
    //  ShowMessage('please selecet ...', 'information');
      alert("select something a category");// lots of ways to do this
    } else {
      var categoryId = selected.val();
      var url = $('#url').data('loadspecificationattributes');
      $.ajax({
        url: url,
        data: {
          categoryId: categoryId,
          controlId: 'specificationattribute'
        },
        type: 'POST',
        success: function(data) {
           // THIS line id does not match my choice of specificationAttribute so I changed it
          $('#specificationAttribute').html(data);
        },
        error: function(response) {
          alert(response.error);
        }
      });
    }
  });

  // THIS should work with the markup I put as an example
  $(document).on('change', '#specificationAttribute', function() {
    alert("changed ");
  });
});// THIS line was missing parts
+4

@Uthman, , , onchange, jsfiddle https://jsfiddle.net/a65m11b3/4/ `

success: function (data) {
        $('#specificationattributes').html(data);
        },and  $(document).on('change', '#specificationAttribute', function () {
        alert("changed ");

    });  $(document).on('change', '#specificationAttribute', function () {
    alert("changed ");

});.
+3

, html .

. , +, ( ). , , , .

: https://api.jquery.com/on/#on-events-selector-data-handler

:

$( "#dataTable tbody" ).on( "click", "tr",      
function() {
console.log( $( this ).text() );
});
-2

All Articles