12

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?

Matt
  • 1,500
  • 8
  • 23
  • 38

4 Answers4

19

console.log($(this).data("events")) in Chrome (or other browsers) would allow you to drill into the object.

Ctrl+Shift+J gets you to the console in Chrome.

ipr101
  • 24,096
  • 8
  • 59
  • 61
16

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());
Jasper
  • 75,717
  • 14
  • 151
  • 146
  • 1
    I saw you had `toString()` in there before... Which was actually a better solution, since that will work on most browsers, where as `toSource()` will not work in IE. – leo.vingi Nov 15 '11 at 21:39
  • I almost removed my answer because I can't explain the difference between `toString()` and `toSource()`. If anyone knows it'd be nice to have them chime in. – Jasper Nov 15 '11 at 21:41
  • 1
    From what I understand, `toSource()` will convert the `object` into `JSON` formatting. W3C: "The toSource() method represents the source code of an object." (http://www.w3schools.com/jsref/jsref_toSource_date.asp) Seems like the `JSON` output is really the key here. – Matt Nov 15 '11 at 21:50
5

If you can't use console.log then alert( $(this).data("events").toSource() ) can also be used.

leo.vingi
  • 1,842
  • 12
  • 17
3

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.

Nikunj K.
  • 8,779
  • 4
  • 43
  • 53