0

im newbie that newbie learn here...

i want add some event from loaded part dynamically but it return to 1 id

so i problem with innerHTML+= and call with ajax like this..

 $.ajax({
                    url: 'getuser.php',
                    type: 'POST',
                    data: {
                        id_user: id_user
                    },
                    success: function(data) {
                       listuser.innerHTML+=data;
                      
                    }
                  
                });

then with getuser.php i got result like this

<div id="listuser">
<button id="1" class="pickmember">Member1</button >
<button id="2" class="pickmember">Member2</button>
</div>

so when i call a function

$(".pickmember").click(function(){
var id_user=$(this).attr('id');
alert('u pick member'+id_user);
});

when i call it,, it always return to last added to innerhtml, and cant detect member2 when click,, there is solution to this?

Jalz Ae
  • 31
  • 1
  • 6

1 Answers1

1

use .on() instead of .click()

$(".pickmember").on('click', function () {
    var id_user = $(this).attr('id');
    alert('u pick member' + id_user);
  });

more information --> jQuery $(".class").click(); - multiple elements, click event once

cabrillosa
  • 155
  • 8