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
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!
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.
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.
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.
use "class" instead of className
$('<div />', { "class": 'test', text: "a div", click: function(e){ e.preventDefault(); alert("test") }})
$(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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With