Posted: 6/9/2010 6:10:16 PM
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 ?
Posted: 6/9/2010 6:18:18 PM
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
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>