4
<script>
  //some code here
  var content = '<div onclick="populatedata(\'' + obj.Records[t] + '\')" >';

  function populatedata(obj) {
    console.log(typeof obj);
  }
</script>

Now output of the above code is string, but I want object content in function populatedata.

5
  • So remove the quotes? Or just pass the index and let your function use the index to look up the right record in the object. Commented Apr 13, 2016 at 18:58
  • @LGSon Then if obj.Records[t] is an object, content will result into <div onclick="[object Object]">. Commented Apr 13, 2016 at 19:04
  • Do you want populatedata to console.log the requested object or a stringified version of it? Commented Apr 13, 2016 at 19:08
  • 1
    Set an event listener using DOM methods instead of injecting it in the HTML string. Commented Apr 13, 2016 at 19:09
  • @Seth I want object in populatedata as I have to process it further. Commented Apr 13, 2016 at 19:09

3 Answers 3

1

As @nnnnnn Suggested I had passed index of record in function and received it in populatedata.

<script>
  //some code here
  var content = "<div onclick="populatedata("+t+")>";

  function populatedata(index) {
    console.log(obj.Records[index]); //this is just for illustration
    //further processing here
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

You didn't clarify what type obj.Records[t] is, but I guess it is an object. So you have problem because result of concatenation with a String is always a new string, with toString() applied to non-string types (obj.Records[t] in your case).

What you need to do is to stringify your object manually so that you control string presentation yourself:

var content = '<div onclick="populatedata(' + JSON.stringify(obj.Records[t]) + ')">';

Then result of such concatenation would be something like (if obj.Records[t] = {name: 123}):

<div onclick="populatedata({"name":123})">

which will allow populatedata invocation with proper parameter.

1 Comment

@dfsq My Data is in this format: {"0":"12","1":"dsferswda","2":"sdfgdsf","3":null,"4":"sdf","5":"sdfdsf","6":"[email protected]","7":"2","8":"True","9":"2016-01-22 21:28:51","ID":"12","TITLE":"dsferswda","SHORT_DESCRIPTION":"sdfgdsf","LONG_DESCRIPTION":null,"APPLY_LINK":"sdf","CONTACT_PERSON":"sdfdsf","EMAILID":"[email protected]","TYPE":"2","ACTIVE":"True","LOGDATE":"2016-01-22 21:28:51"}
0

If you want to be able to further process the argument of populatedata, you'll first want to stringify it, then change your function from logging the typeof to just the object.

var content = "<div onclick='populatedata(" + JSON.stringify(obj.Records[t]) + ")'>test</div>";
function populatedata(obj) {
  // do things with obj here
  console.log(obj);
}

However, as Oriol mentioned in a comment on your question, you probably don't want to take that approach. It's better to:

  • Manage handlers through the dom API
  • Pass data objects in programmatically as opposed to embedding them into the dom

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.