How to create div with class

fteinz picture fteinz · Oct 20, 2011 · Viewed 57.5k times · Source

I'm trying to create a div and give him a class but it doesn't work. Could anybody help me?

$(document).ready(function() {
$('input[type=checkbox]').each(function() {
    $(this).after($('<div />', {
        className: 'test',
        text: "a div",
        click: function(e){
            e.preventDefault();
            alert("test")
        }})); 
    });
});

The css:

   .test {
    width:200px;
    height:200px;
    background-color:#eeeeee;
    }

at the moment he creates the div but the color isn't #eeeeee

Answer

Corneliu picture Corneliu · Oct 20, 2011

use "class" instead of className

$('<div />', {
        "class": 'test',
        text: "a div",
        click: function(e){
            e.preventDefault();
            alert("test")
        }})