Altay Mazlum Altay Mazlum - 1 year ago 49
CSS Question

How to change the css of the div that i just created in code

I have the code just below, I don't even know if it works but I just wanted to learn how to change the css properties of the div that I just created within my JQuery structure. Like changing it's width, color and stuff.

jQuery(document).ready(function () {
jQuery("#button").click(function () {
jQuery("#button").html('Click me again');

jQuery(".button1").click(function () {
jQuery('<div />')
'width': '150px',
'height': '150px',
'background-color': 'red'

Any help will be appreciated.

Answer Source

Use css() to add styles to the element.

See the comments inline in the code:

jQuery('<div />') // Create new div element
    .addClass('buttondiv') // Add new class
    .css({ // Add inline styles
        'background-color': 'red',
        'color': 'green'
    .appendTo("#maindiv"); // No need of `jQuery()` here