Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change event doesn't work on dynamically generated elements - Jquery

I generate a dropdownList dynamicly with jquery Ajax , generated dropdown's id is specificationAttribute . I want create add event for new tag was generated (specificationAttribute) , to do this I created Belowe script in window.load:

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

but it does not work . I try any way more like click , live but I cant any result.

jsfiddle

Code from fiddle:

$(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 ");

    });
    }
like image 805
Uthman Rahimi Avatar asked Apr 23 '16 05:04

Uthman Rahimi


2 Answers

Your fiddle has syntax errors. Since a dropdownlist generates a select, let's use one.

For my answer I used THIS HTML, more on this later: things did not match in your code

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

Code updated: (see inline comments, some are suggestions, some errors)

$(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
like image 92
Mark Schultheiss Avatar answered Oct 19 '22 21:10

Mark Schultheiss


@Uthman, it might be the case that you have given different id to select and using wrong id in onchange event as i observed in the jsfiddle link 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 ");

});.
like image 28
kamlesh pandey Avatar answered Oct 19 '22 21:10

kamlesh pandey