Uthman Rahimi
Uthman Rahimi

Reputation: 708

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

    });
    }

Upvotes: 6

Views: 5685

Answers (3)

kamlesh pandey
kamlesh pandey

Reputation: 185

@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 ");

});.

Upvotes: 4

Mark Schultheiss
Mark Schultheiss

Reputation: 34168

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

Upvotes: 4

tlt
tlt

Reputation: 15211

It doesnt work because at the moment of attaching event your html element doesnt existi yet.

What you need are delegated events. Basically, you attach event to parent element + you have selector for child (usually by classname or tagname). That way event fires for existing but also for elements that meet selector added in future.

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

Especially part with this example:

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

Upvotes: -2

Related Questions