Is there a way to see object contents using Javascript?

Posted on 2009-02-13
Last Modified: 2012-05-06
Hello everybody!

I am using javascript and I debugging a script.  I am using Firebug to debug it.  Whenever I output an object it say something like [object Object].  Is there an easy way to output the contents of an object.  So I can see the properties and methods in the form of a string or whatever it contains?

Question by:parlays
    LVL 16

    Expert Comment

    You can loop thru the properties of an object using a for loop:
    for (var i in object)

    Is that what you're looking for?
    LVL 15

    Assisted Solution

    in FF3 by alert object, it should able to show what type of object/element is...
    But also, you could got the properties by obj.nodeName, obj.type, obj.value .........
    please visit , it would show how to get attributes from an object/element
    please try code snippet
    <script language="javascript">
    function alertme(obj){
    alert("obj.nodeName : " + obj.nodeName + " obj.type : " + obj.type + " obj.value : " + obj.value + " obj.nodeType : " + obj.nodeType + " obj.attributes['privatevalue'].value : " +obj.attributes['privatevalue'].value );
    <form name="form1">
    <input type="button" privatevalue="testing" onclick="alertme(this);" value="check this element properties">

    Open in new window

    LVL 6

    Expert Comment

    tyy alert(objectname);
    LVL 41

    Accepted Solution


    Put the attached file someplace convenient (e.g., in the same directory as your other JavaScript code, or in the same folder as your HTML).

    Add a <script ...> tag to include the code

    Add a <div id='debug'></div> to the end of your page

    and then add code like shown below to display the contents of the specified object ("obj" in the example).

    If you only want the "string" attributes display, add a 4 as the 3rd, optional parameter:

    There is an optional 3rd parameter that defines what kind of attributes should be displayed.  This "flags" parameter is a combination of the following bit fields:

    Mask:  Binary    Dec   typeof()                              
          --------   ---   --------                              
          00000001 =   1 = boolean                              
          00000010 =   2 = number                                
          00000100 =   4 = string                                
          00001000 =   8 = function                              
          00010000 =  16 = object                                
          00100000 =  32 = other                                
          01000000 =  64 = constants     name matches /^[A-Z_]+$/
          10000000 = 128 = TBD                                  

    So, to display "strings" and "functions" just add 4 + 8 = 12, and have this value (12) as the 3rd parameter.

    Does this make sense?
    <script type='text/javascript' src='objDisplay.js'></script>
      var debug = document.getElementById( 'debug' )
      if ( debug ) {
          // If the debug "div" exists, display the entire object contents
        debug.innerHTML = objDisplay( 'objectName', obj )
      // if you only want to display the string attributes, the call would be:
      debug.innerHTML = objDisplay( 'objectName', obj, 4 )

    Open in new window

    LVL 41

    Expert Comment

    Thanks for the grade & points.

    Good luck & have a great day.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
    PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now