Skip to main content
  1. About
  2. For Teams
Asked
Viewed 62k times
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?

0

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

1 Comment

@scott How do I do it with Chrome?
13

Just convert your object to a JSON object using stringfy.

alert(JSON.stringify(yourObjectVariable));

simple as pie :)

Comments

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.. :)

1 Comment

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!
3
alert(JSON.stringify(YOUR_OBJECT_HERE, null, 4));

1 Comment

Donald Duck is with Ukraine
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.
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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.