Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dump jquery object in an alert box

I am not quite adept in maneuvering jQuery, and it came to a point that I need to debug a program that was passed down from me without a documentation.

I have this var a, an object, that I really want to know the content of its collection. In my mind I need a function like foreach() in PHP to iterate over this object variable. Upon researching I end up in using jQuery.each(). Now I can clearly iterate and see what was inside var a.

However, it was kind of annoying to alert once every value on the var a. What I wanna know if it's possible to display all the contents in just one pop of alert box?

Here is my code:

$.each(a, function(index, value) { 
alert(index + ': ' + value); 
});

The var a contains infos such as:

creationdate: date_here
id: SWFUpload
modificationdate: date_here
type: .jpg
index: 0
name: uploaded_filename.jpg
size: size_in_bytes

BTW: The var a is called via file upload script.

like image 273
planet x Avatar asked Jan 03 '12 06:01

planet x


People also ask

How do you print an object of an object in alert?

Using Window. alert() method displays a dialog with the specified content. Printing an object using the Window. alert() method will display [object Object] as the output. To get the proper string representation of the object, the idea is to convert the object into a string first using the JSON.

Can we call alert from jQuery?

Unfortunately there is no built in one-liner for displaying a simple alert in jQuery UI, but it is easy to add an own one. This is a standard jQuery UI dialog displayed using one line of code. It follows the same theme as the rest of the jQuery UI widgets used on the site and it behaves like a normal modal dialog.


2 Answers

Why don't you just accumulate the values in an array, then display the whole array (for instance, using JSON)? Example:

var acc = []
$.each(a, function(index, value) {
    acc.push(index + ': ' + value);
});
alert(JSON.stringify(acc));

In any case, I'd suggest using a debug tool like Firebug. So you could just use console.log(a) and be able to navigate freely through the objects's fields.

like image 90
mgibsonbr Avatar answered Oct 01 '22 02:10

mgibsonbr


In firefox you could try:


alert(yourObject.toSource());

OR you could use some plugin: See: jQuery Dump Plugin

like image 29
Sudhir Bastakoti Avatar answered Oct 01 '22 00:10

Sudhir Bastakoti