Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display object contents - JS/jQuery

With $(this).data("events"); returning [object Object], I need to see what's actually going on in there. I found this:

var Finder = "";
$.each($(this).data("events"), function(i, n){
    Finder += "Name: " + i + ", Value: " + n + " | ";
});

However, n still returns [object Object]:

EDIT: (Output) --

Name: click, Value: [object Object] | 

--

Is there an efficient way to show everything inside that sucker, kind of like print_r in PHP?

like image 273
Matt Avatar asked Nov 15 '11 21:11

Matt


People also ask

How do I print the contents of an object in JavaScript?

stringify() method is used to print the JavaScript object. JSON. stringify() Method: The JSON. stringify() method is used to allow to take a JavaScript object or Array and create a JSON string out of it.

How do I print an object in console?

Answer: Use console. log() or JSON. stringify() Method This method will print the object in browser console.


2 Answers

You can use .toSource() to turn JavaScript objects into a string representation that you can view without a nice error console like in Firebug or Chrome Dev. Tools:

alert($(this).data("events").toSource());
like image 60
Jasper Avatar answered Oct 18 '22 14:10

Jasper


Print content of object you can use

console.log(obj_str);

you can see the result in console like below.

Object {description: "test"} 

For open console press F12 in chrome browser, you will found console tab in debug mode.

like image 42
Nikunj K. Avatar answered Oct 18 '22 16:10

Nikunj K.