I want to get cell value from a table by clicking the button
. I already made a getDetail
function using Javascript implemented on the button, but I cannot return the value from the function.
<button class='btn btn-secondary' onclick='getDetail()'>Detail</button>
Here is my Javascript function to get the detail from the table:
function getDetail(){
// get element value
var table = document.getElementById("tableID");
var myResult = {"db":"postgres"};
if (table) {
for (var i = 0; i < table.rows.length; i++) {
table.rows[i].onclick = function() {
tableText(this);
};
}
}
function tableText(tableRow) {
var schName = tableRow.childNodes[1].innerHTML;
var tblName = tableRow.childNodes[2].innerHTML;
var obj = {'table_name': tblName};
return obj;
}
let myData = Object.assign(myResult, obj);
console.log(myData);
$.ajax({
error:function (xhr, status, error) {
var err_msg = ''
for (var prop in xhr.responseJSON) {
err_msg += prop + ': ' + xhr.responseJSON[prop] + '\n';
}
console.log(err_msg)
},
success: function(){
console.log(obj)
}
});
}
I want to return obj value as an object. After that, I will combine it with other objects as an input in my AJAX function (later it will be combined with API response).
However, I cannot return the obj value from the previous function.
Error:
dashboard:511 Uncaught ReferenceError: obj is not defined
at Object.success (dashboard:511)
at c (VM10029 jquery.min.js:2)
at Object.fireWith [as resolveWith] (VM10029 jquery.min.js:2)
at l (VM10029 jquery.min.js:2)
at XMLHttpRequest.<anonymous> (VM10029 jquery.min.js:2)
Expected output when button is clicked:
{db: 'postgres', table_name: 'actor'}
Could you please help me to solve this problem? because I am a new learner with javascript. Thank you.