13

I am trying to study the jquery class, but I have a hard time debugging an object because I can't see the element inside of it

$("#birds").autocomplete({
    source: "search.php",
    select: function (event, ui) {
        alert(ui);
    }
});

it returns [object Object].. :( My question is how can I alert the object so that I can see the element?

Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
mapet
  • 868
  • 1
  • 11
  • 21

6 Answers6

20

i recommend you use FireBug for debugging javascript. then you can just do

console.log(ui) 

and it'll log the object in a form you can expand

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
11

Just convert your object to a JSON object using stringfy.

alert(JSON.stringify(yourObjectVariable));

simple as pie :)

Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
Shafin Mahmud
  • 3,831
  • 1
  • 23
  • 35
4

you can also try Java Script method:

 // Alert javascript object in alert box
    function alertObject(obj){      
        for(var key in obj) {
        alert('key: ' + key + '\n' + 'value: ' + obj[key]);
        if( typeof obj[key] === 'object' ) {
            alertObject(obj[key]);
        }
        }
    }

Here 'obj' is:

// your object var
var getObject = {};

// object set with key an val
getObject.swfVersionStr = '10.0';
getObject.xiSwfUrlStr = null;
getObject.flashvarsObj = {};
getObject.parObj = {allowfullscreen: "true",wmode: "window",menu: "false"};

Call like this:

alertObject(getObject );

So, simple.. :)

Nono
  • 6,986
  • 4
  • 39
  • 39
  • thanks singh, this is the best way to show object in alert. there are lot of situations when you dont want to use console.log or is unavailable. Great! – khunshan Feb 17 '14 at 12:55
3
alert(JSON.stringify(YOUR_OBJECT_HERE, null, 4));
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Nov 21 '20 at 10:17
1

If you are using Firefox then you can alert object value like below code

 alert(object.toSource());   // for you alert(ul.toSource());

That above code worked fine for me.

thecodedeveloper.com
  • 3,220
  • 5
  • 36
  • 67
1

Convert your array or object to a JSON object using stringify.

Example:

var obj = { "name":"bayiha", "age":30, "city":"Eseka"};
var myJSON = JSON.stringify(obj);

alert(myJSON);

for more info clik here

Kokogino
  • 984
  • 1
  • 5
  • 17
Rock Dial
  • 129
  • 1
  • 2