Jaber Jaber - 1 month ago 6
jQuery Question

jQuery - Use a variable as a function parameter

I want to use dynamic variable as a dynamic javascript function parameter. But it's giving error as variable not defined.

$.ajax({
url: 'get_category_list.php',
dataType: 'JSON',
success: function (data) {
var i = 1;
$.each(data, function() {
var id = this.id;
//console.log(id);
var edit = "<img src='../../dist/img/icon/edit.png' " +
"style='border: 1px solid #999637; " +
"border-radius: 2px' " +
"title='Edit' " +
"onclick='show_edit_form(id)'>";
catTable.row.add([
i++,
this.name,
this.level,
this.create_date,
edit
] ).draw();
});
}
});


Please tell me what is the exact problem here?

Answer

I guess the error occurs in show_edit_form function. You have to pass something there, in your care it's

 var edit = "<img src='../../dist/img/icon/edit.png' " +
            ...
            "onclick='show_edit_form(" + this.id + ")'>";