0

I'm creating an element using jquery then appending it to another one. I'm trying to style my header but it doesn't seem to be working for some reason. There doesn't seem to be any syntax errors. It's appending to the div and the text is appearing, it's just not being styled.

 jQuery('<h3/>', {

text: 'Todays news',
css: ('background-color', 'red'),
id: 'tdn'

}).appendTo('#div1');

Any help would be appreciated

4 Answers 4

1

Try to wrap your CSS style inside { } instead of ( )

jQuery('<h3/>', {
    text: 'Todays news',
    id: 'tdn',
    css: {'background-color': 'red'},
}).appendTo('#div1');

Fiddle Demo

or you can use .css() as well:

jQuery('<h3/>', {
    text: 'Todays news',
    id: 'tdn'
}).css('background-color', 'red').appendTo('#div1');

Fiddle Demo

Sign up to request clarification or add additional context in comments.

Comments

1

Try using {} instead of () for your css parameters

$('<h3/>', {

text: 'Todays news',
    css: {'background-color': 'red'},
id: 'tdn'

}).appendTo('#div1');

http://jsfiddle.net/xerxesnoble/e93Hj/

Comments

1

You can do this:

jQuery('<h3/>', {
    text: 'Todays news',
    id: 'tdn'
    style: 'background-color:red'
}).appendTo('#div1');

Demo

Comments

1

use style: instead of css:

 jQuery('<h3/>', {

text: 'Todays news',
style: "background-color:red",
id: 'tdn'

}).appendTo('#div1');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.