taufik
taufik

Reputation: 269

Javascript variable returns undefined

I want to set value for kode_lahan same with kode, I send value kode when I click delete button. But, when I run my code, kode_lahan value is undefined

function tampilLahan(){
        $.ajax({
            type  : 'ajax',
            url   : base_url+"investasi/tampilDataPermintaan",
            async : true,
            dataType : 'JSON',
            success : function(data){
                var html = '';
                var i;
                for(i=0; i<data.length; i++){
                  var kode = data[i].kode;
                    html += '<tr>'+
                            '<td>'+kode+'</td>'+
                            '<td>'+
                            '<a href="javascript:void(0);"class="btn btn-danger item_delete" kode="'+kode+'">Delete</a>'+
                            '</td>'+
                            '</tr>';
                }
                $('#data').html(html);
            }
        });
    }

  $('#data').on('click','.item_delete',function(){
  var kode_lahan = $(this).data("kode");
  console.log(kode_lahan);

    });

I expect the output of kode value

Upvotes: 2

Views: 44

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44145

You're setting the kode attribute in your AJAX, but looking for the data-kode attribute - it's better to go with data-kode because kode isn't a valid attribute name, so rename it when it's being set.

'<a href="javascript:void(0);"class="btn btn-danger item_delete" data-kode="'+kode+'">Delete</a>'+

Upvotes: 2

Related Questions