Thursday, April 3, 2008

Javascript - properties for any HTML object

Sometimes you need to see what properties can you use for any given object.
Just copy-paste this:



var stuff = '';
for (p in document.Form1.elements)
stuff = stuff + ' ' + p;
alert(stuff);



( ... and replace the document.form1.elements with anything you'd like. For example document.body, or document.getElementById("someTDfromYourTable"), etc. ...)

2 comments:

Radulii said...

I use the more complex (and with more info)

var obj = any object you want
var str = ""; //final string to show

for (var prop in obj)
{
str += "obj." + prop + " = " + obj[prop] + "
";
}

someDiv.innerHTML = str;

this will also show you the value of each property and you can change as needed.
you might want to htmlEncode the obj[prop] string ;)

Radulii said...

the str += "obj." + prop + " = " + obj[prop] + "
";

line from above is actually a

str += "obj." + prop + " = " + obj[prop] + "<br />";

but who knew :D