39

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

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
fteinz
  • 1,085
  • 5
  • 15
  • 30

5 Answers5

64

use "class" instead of className

$('<div />', {
        "class": 'test',
        text: "a div",
        click: function(e){
            e.preventDefault();
            alert("test")
        }})
Corneliu
  • 2,932
  • 1
  • 19
  • 22
6
$(document).ready(function() {
$('input[type=checkbox]').each(function() {
    $(this).after($('<div />', {
        class: 'test',
        text: "a div",
        click: function(e){
            e.preventDefault();
            alert("test")
        }}));
    });
});

http://jsfiddle.net/yF9pA/1/

alexl
  • 6,841
  • 3
  • 24
  • 29
5
 $('<div>', { 'class': 'your_class' })
                 .load('HTML Structure', CallBackFunction())
                 .appendTo(document.body);
Dipak
  • 6,532
  • 8
  • 63
  • 87
3
$('input[type=checkbox]').each(function() {

$(this).after('<div></div>').addClass('test')
  .filter('div').html('a div')
.click(function() {
  alert('Handler for .click() called.');
}).end()
.appendTo('this');

});

Should work :)

Marco Johannesen
  • 13,084
  • 6
  • 31
  • 36
2

try class instead of className

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93