Dadan Hamdani Dadan Hamdani - 3 years ago 196
Javascript Question

How to pass multiple variables from ajax to function in javascript?

How can I pass 2 variables from ajax to function ?
I can successfully pass 1 variable, but I don't know how to write the code to pass 2 variables. I'm stuck here:

<button id ="+data[i].id+" onclick='tahu("+data[i].id+","+data[i].nama+")'>Detail</button>


Here is my function

function tahu(x, y) {
window.alert(x);
window.alert(y);
}


Here is my full ajax code

function tampilkan() {
var data_table = "";
var head_table = "";
$.ajax({
url: "showkaryawan/da",
dataType: "json",
success: function(data) {
$('#oo').empty();
head_table +="<thead><tr class='bg-info'><th width='10%'>ID Karyawan</th><th width='30%'>Nama Karyawan</th><th width='15%'>Action</th></tr></thead>";

for (var i =0; i<data.length; i++) {
data_table +="<tr><td>"+data[i].id+"</td><td>"+data[i].nama+"</td><td>"+"<button id ="+data[i].id+","+data[i].nama+" onclick='tahu("+data[i].id+")'>Detail</button>"+"</td></tr>";
}

$('#oo').append(head_table);
$('#oo').append(data_table);
}
});
}

Answer Source

this is not an answer, just another method to finish my goal. now i'm using data table, thanks;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download