0

I have introduce a part of the html by Ajax.

my html

<div class="article-feed">
    // html is loaded here.
</div>

ajax

var url = 'getpageplaceinfo/0';
$.ajax({
    type:'GET', 
    url: url, 
    dataType: 'html'
}).done(function(data){
    $('.article-feed').append(data); 
}   

It load the html like this here

myname        
<input type="checkbox" name="place" value="1">

it load the checkbos correctly, but when I tried to get the move of checkbo , it doesn't work

$('input[name=place]').on('change', function(){
// not being called

Is it correct behaivor??

or How can I handle the dynamically loaded input??

Antoine Delia
  • 1,728
  • 5
  • 26
  • 42
whitebear
  • 11,200
  • 24
  • 114
  • 237
  • Possible duplicate of [Event binding on dynamically created elements?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – VLAZ Oct 09 '19 at 13:35

3 Answers3

2

This is because the event as you defined is bound when the code first runs and will not work on dynamic objects created after that time.

You can alternatively bind to dynamic controls using the following:

$('body').on('change', 'input[name=place]', function() {...

This will detect any object (dynamic or static) matching the selector input[name=place].

Martin
  • 16,093
  • 1
  • 29
  • 48
0

You should attach your listener after the HTML has been appended.

    var url = 'getpageplaceinfo/0';
    $.ajax({
        type:'GET', 
        url: url, 
        dataType: 'html'
    }).done(function(data){
        $('.article-feed').append(data); 
        $('input[name=place]').on('change', function(){
            // handle change
        }
    }   

Jay Kariesch
  • 1,392
  • 7
  • 13
-1

Have you tried using this?

$('input[name=place]').on('click', function(){})
Olalekan
  • 185
  • 1
  • 10