Who is online?  212 guests and 0 members
home »forums »asp.net topics »client side web development »Print all the properties names and values of properties of a jasvascript object

Print all the properties names and values of properties of a jasvascript object

Topic RSS Feed

Posts under the topic: Print all the properties names and values of properties of a jasvascript object

Posted: 6/9/2010 6:10:16 PM

Lurker 230  points  Lurker
  • Joined on: 10/16/2009 1:00:00 PM
  • Posts: 46

Hi Experts ,

 

I need to print all the properties names and values of properties of a jasvascript object. Please help me how to do so ?


javascript

Posted: 6/9/2010 6:18:18 PM

Contributor 2499  points  Contributor
  • Joined on: 5/3/2010 8:45:04 PM
  • Posts: 145
answered  Answered

Hi kevin.

Here is a solution:

Add one button in your ASPX page

ex:

        <asp:Button ID="Button1" OnClientClick="return getProperties()" runat="server" Text="Get Object Properties" Width="278px" />


then, in your <head> ... </head> add this JavaScript function

    <script type="text/javascript" language="javascript">        
    function getProperties() {
        var myObject = new Object(); //Create new object
        var objPropertiesString = "";
        myObject["firstName"] = "Hajan";
        myObject["lastName"] = "Selmani";
        
        for(p in myObject)
        {
            objPropertiesString += p + " - Value :" + myObject[p] + "\n";        }
        alert(objPropertiesString); //Show all properties and its value
        }
    </script>


Analyze the code. If you have anything to ask, write back :).

Regards,
Hajan


Posted: 6/11/2010 7:01:20 PM

Professional 10133  points  Professional
  • Joined on: 4/19/2009 1:46:52 AM
  • Posts: 241
answered  Answered

Nice Hajan,

Me too was going for similar approach:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">

        var obj = new Object();
        obj.a = 'a1';
        obj.b = 'b1';
        obj.c = 'c1';
        for (f in obj) {
            document.write('o.' + f + ' = ' + obj[f]);
            document.write('<br/>');
        }
       
    </script>
</head>
<body>
</body>
</html>



javascript
Page 1 of 1 (3 items)