1

i am calling this function

   el=DOM.create({tag:"div",css:{backgroundColor:"#000000",color:"green"},html:"abc"});
   $("#fb_dv").append(el);

my function is here

DOM={
    create:function(data){
        if(data.tag){
            d=document;
            doc=d.createElement(data.tag);
            for(var v in data){
                 if(v=="css"){
                     for(key in data[v]){
                        prop=doc.style;
                        prop.key=data[v][key]
                     }
                 }
                 if(v=="html"){
                     doc.innerHTML=data[v]
                 }
            }

            return doc;
        }
    }
}

The problem is that this function is not setting css properties

2
  • 1
    The second line, using $.append() is your own library? Commented Apr 13, 2012 at 14:58
  • yes i have created it but problem is with DOM function Commented Apr 13, 2012 at 14:59

1 Answer 1

3

The problem is in the for loop that should set the styles. You need to use the square bracket syntax to set the correct property:

for(key in data[v]){
    prop = doc.style;
    //prop.key = data[v][key]... change this to this:
    prop[key] = data[v][key];
}

Here's a working example.

As a side note, you have a lot of missing var statements so all of your variables are leaking into the global scope, and you also have a lot of missing semi-colons.

Sign up to request clarification or add additional context in comments.

1 Comment

You are missing them after prop.key=data[v][key], doc.innerHTML=data[v] and after the closing } of DOM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.