0

Here is few line of java script code to create a div element dynamically. But my doubt is after create that element, is there any short-cut way to assign multiple css rules instead of typing (container.style) again and again.

<script>
    (function(){
        window.onload = function(){
            var container = document.createElement('div');
            container.style.width = '500px';
            container.style.height = '200px';
            container.style.backgroundColor = 'red';
            container.style.margin = '0 auto';

            document.body.appendChild(container);
        }
    }());
</script>
3
  • 3
    why don't you define the styles in CSS? Commented Jan 4, 2013 at 8:00
  • Look into jQuery, it has a method .css() which you can use to apply many css styles. Commented Jan 4, 2013 at 8:01
  • In jquery it is very easy. but is there any short-cut for javascript also. Commented Jan 4, 2013 at 8:06

4 Answers 4

5

Use a CSS class and assign the class to the element:

.MyClass {
   width: 500px;
   height: 200px;
   background-color: red;
   margin: 0 auto;
}

Then container.className = "MyClass";

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

Comments

1

You could use a for loop to iterate over an object, applying the properties to the style property one-by-one:

(function(){
    window.onload = function(){
        var container = document.createElement('div'),
            styles = {
                "width": "500px",
                "height": "200px",
                "backgroundColor": "red",
                "margin": "0 auto"
            },
            i;
        for (i in styles) {
            container.style[i] = styles[i];
        }

        document.body.appendChild(container);
    }
}());​

It's worth noting that this is longer than the original code :-p If you wrap the for loop in another function and re-use the function repeatedly it could be useful. If you find yourself needing this repeatedly, I would highly recommend including jQuery (or ZeptoJS) and using .css(…) to apply the CSS. jQuery does several transformations to the properties as it applies them… It changes hyphens in the property names to the correct camel casing, makes changes to apply the correct properties or property values for IE, etc. It makes the coding much easier, and any future code maintainers will benefit from that ease as well.

Comments

0

You can use like this

var container = document.createElement('div');
                container.setAttribute("id","div1");
                document.body.appendChild(container);

$("#div1").css({"width":"500px","height":"200px","background-color":"red"});

Demo

Comments

0

You could write your own css function:

<script>
    (function(){
        window.onload = function(){
            var container = document.createElement('div');
            css(container,
                {'width': '500px',
                 'height': '200px',
                 'backgroundColor': 'red',
                 'margin': '0 auto'});

            document.body.appendChild(container);
        }

        function css(element, style){
            for(key in style){
                if(style.hasOwnProperty(key)){
                    element.style[key] = style[key];
                }
            }
        }
    }());
</script>

You can then re-use the css() function for other elements.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.