Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create div with class

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

like image 889
fteinz Avatar asked Oct 20 '11 09:10

fteinz


People also ask

Can we use class in div?

The <div> tag is used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript. The <div> tag is easily styled by using the class or id attribute. Any sort of content can be put inside the <div> tag!

Is a div a class?

A div is a container tag that is used to define a division or a section in an HTML document, whereas a class is an attribute that specifies actions to be taken to one or more elements.

Why do we use class in div?

Divs, Spans, Id's, and Classes You use them to divide or label your HTML (when another, more semantic tag will not work) and use CSS selectors to target them. Class and Id's are HTML Attributes. They are also used as CSS hooks.

How do I put two classes in a div?

To define multiple classes, separate the class names with a space, e.g. <div class="city main">. The element will be styled according to all the classes specified.


2 Answers

use "class" instead of className

$('<div />', {         "class": 'test',         text: "a div",         click: function(e){             e.preventDefault();             alert("test")         }}) 
like image 135
Corneliu Avatar answered Sep 28 '22 10:09

Corneliu


$(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/

like image 31
alexl Avatar answered Sep 28 '22 09:09

alexl