6

I am creating a table row dynamically. Each row is having onclick event. When I click on that table row I want to pass row object to a function but my problem is, while passing that object, I am getting [object object] string. So I am not able to use that object in function.

Please give some solution thanks in advance.

This is my code:

  var row;
  $.each(mydata, function(i,data){
            row+='<tr onclick="myfunction(\''+data+'\')"><td >data.name</td><td >data.age</td></tr>;
    });
 $("#myTable").append(row);
5
  • what is "row" in your code Commented Mar 31, 2015 at 7:36
  • sorry now I changed my code .row is a variable and data is a object Commented Mar 31, 2015 at 7:42
  • something like this.. <tr onclick="myfunction("[object Object]'')>....? Commented Mar 31, 2015 at 7:43
  • yeah... I am getting like this.I am not able to use this.What to do. Commented Mar 31, 2015 at 7:46
  • By that way..it wont be possible as everything will be converted finally to string... Commented Mar 31, 2015 at 7:49

3 Answers 3

1

I would better use jQuery for defining click event handlers. Here is your updated code:

var $table = $("#myTable");
$.each(mydata, function(i,row){
        $tr = $('<tr>').appendTo($table);
        $tr.on("click", myfunction);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Convert the data object to the following style string.

"{\"name\": \"lenient\", \"age\": 18}" 

Then bind it to the click event, you will get the object as the parameter inside the click function.

Comments

0

Try adding JSON.stringify()

onclick="myfunction("+JSON.stringify(data) +")"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.