Prady Prady - 1 month ago 15
Javascript Question

what does [object Object] mean?

I am trying to alert a returned value from a function and i get this in the alert

[object Object]

here is the javascript code

<script type="text/javascript">
$(function ()
{
var $main = $('#main'),
$1 = $('#1'),
$2 = $('#2');

$2.hide(); // hide div#2 when the page is loaded

$main.click(function ()
{
$1.toggle();
$2.toggle();
});

$('#senddvd').click(function ()
{
alert('hello');
var a=whichIsVisible();
alert(whichIsVisible());
});


function whichIsVisible()
{
if (!$1.is(':hidden')) return $1;
if (!$2.is(':hidden')) return $2;
}

});

</script>


whichIsVisible is the function which i am trying to check on

Answer

The default conversion from an object to string is "[object Object]".

As you are dealing with jQuery objects, you might want to do

alert(whichIsVisible()[0].id);

to print the element's ID.

As mentioned in the comments, you should use additional tools like Firebug for Firefox to introspect objects by doing console.log(whichIsVisible()) instead of alert (no need for extra tools in Chrome or Safari).

Sidenote: IDs should not start with digits.

Comments