Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

364
Views
How to get return value as an object from a click function (Javascript)?

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.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are not returning the value in function call

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() {
          //modified code         
          return tableText(this);
                };
            }
        }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!