Get all custom DOM properties

I am attaching DOM properties to an element dynamically like so:

i = 0;
document.body['a' + i] = "foo";
document.body['b' + i] = "bar";

Is there a way I can get all of those properties that I attached as an array? Example:

var allProperties = ['a0', 'b0'];

Thanks!

Answers


You can actually create a new body object, and compare against that object to get properties not added by the browser

i = 0;
document.body['a' + i] = "foo";
document.body['b' + i] = "bar";

var el  = document.createElement('body'),
    arr = [];

for (var key in document.body) {
    if (document.body.hasOwnProperty(key) && !(key in el)) {
        arr.push(key);
    }
}

FIDDLE

being a little more fancy

var el  = document.createElement('body'),
    arr = Object.keys(document.body).filter(function(prop) {
        return !(prop in el);
    });

Add a property with a fixed name to the element you're interested in, and push the custom properties

var pushInto = function(element, name, value)
{
    element[name] = value;
    if (element["customProperties"] == undefined)
    {
         element["customProperties"] = [];
    }
    element["customProperties"].push(name);
}

Then you can do :

pushInto(document.body, "name", "value");
document.body["customProperties"]; // ["name"]

Need Your Help

update Model from UserControl - mvc

asp.net-mvc asp.net-mvc-3

I have a problem: I Have created a UserControl A, A has its own controller in order to reuse Usercontrol actions in every view added, UserControl A expects a model UserControlModel, what I want to ...